Regex matches hexadecimal digits

/^((0x[0-9a-f]+)|(\$[0-9a-f]+)|([0-9a-f]+h))$/i
/^(((0x|\$)[a-f0-9]+)|([a-f0-9]+h))$/i

/ indicates the start and end of regexp in JS, that is, it does not belong to the regular expression itself.
i is a regex flag used to make the match case-insensitive, i.e. it matches 0xA, 0Xa, aH and Ah. If you don't want to match 0X prefix or H suffix, you have to remove the i flag and explicitly add AF to the regex number part
^ and $ are the beginning and end of the string. Therefore, it only matches a complete string, not when the regex is part of a larger string. That is, matches "0xffff", but not "foo 0xffff bar"
((0x|$)[a-f0-9]+) matches a valid hexadecimal number prefixed with 0x or . Since is a valid hexadecimal number for the prefix. becauseA valid hexadecimal number prefixed with . Since it is a special character in regular expressions, it must be escaped with \ for verbatim matching.
([a-f0-9]+h) matches a valid hexadecimal number followed by an h

Guess you like

Origin blog.csdn.net/weixin_43706224/article/details/129282779