I find your replacement code quite convoluted
Try this:
Lua Code:
link:gsub("|H(%x%x%x%x%x%x)(.-|h)(.-)|h", "|H0%2|h")
-- first capture matches six hex digits, which are discarded
-- second capture matches everything up to and including |h, and is put in replacement %2
-- third capture matches everything up to second |h (link text, discarded)
-- you can use it on old and new links alike, no need to test which format you have
-- on new links it wil just not replace anything, because the |Hxcolor code won't match
edit: oops, somehow I dropped one x from the pattern. fixed