I’m trying to use the following code with jQuery to validate hex value strings but I get unexpected results:
var a = new RegExp("0x[a-fA-F0-9]+")
var result = a.test('0x1n')
In this case, result actually returns true. What am I missing here?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need anchors to match the beginning and the end of the string. This will make the regular expression try to match against the entire string instead of just a part of the string:
Otherwise your regular expression matches the
0x1part and returnstrue.On another note, the following would be a little better:
The
iflag makes it case insensitive so you don’t have to specifya-fandA-F.