By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,818 Members | 2,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,818 IT Pros & Developers. It's quick & easy.

Hexidecimal regular expression

P: n/a
What is the regular expression that checks that every character in a
string is in [0-9][A-F] ?

Thanks.

Sep 5 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2006-09-05, gi*******************@yahoo.com <gi*******************@yahoo.comwrote:
What is the regular expression that checks that every character in a
string is in [0-9][A-F] ?
mystring="abcd1023"
reg=/^[0-9A-F]*$/i
print(reg.test(mystring))

^: Start
$: End
[0-9A-F]: Hex characters, but only capitals
"/i": ignore case (to get lower case characters as well)
(one could use [0-9A-Fa-f] to check upper and lower explicitly)
print(.): I am using the Firefox spider monkey (Javascript 1.5[*])
interpreter (which uses print(.) to output results).
One a web page, use alert(.) instead of print(.).
*: There is a 1.6 version out, but the interpreter is not
standalone but requires the installation of the libraries.
I have not installed that one yet.

/^[0-9A-Fa-f]*$/i

Beginning of line, then an string of any number (even zero)
hex characters (ignore case) and then end of line (so ONLY
hex characters - i.e. a line made up only of hex chars).
Sep 5 '06 #2

P: n/a
On 2006-09-05, Spamless <Sp******@Nil.nilwrote:

Sheesh ... why double the test for each case?

/^[0-9A-F]*$/i or
/^[0-9A-Fa-f]*$/

(no need for "/i" if you put in a-f along with A-F)
Beginning of line, then an string of any number (even zero)
hex characters (ignore case) and then end of line (so ONLY
hex characters - i.e. a line made up only of hex chars).
Sep 5 '06 #3

P: n/a
Spamless wrote on 05 sep 2006 in comp.lang.javascript:
> Beginning of line, then an string of any number (even zero)
hex characters (ignore case) and then end of line (so ONLY
hex characters - i.e. a line made up only of hex chars).
[please do not toppost on usenet]
On 2006-09-05, Spamless <Sp******@Nil.nilwrote:

Sheesh ... why double the test for each case?

/^[0-9A-F]*$/i or
/^[0-9A-Fa-f]*$/

(no need for "/i" if you put in a-f along with A-F)
when testing, try the inverse,
and you only need to find 1 character disbehaving,
no begin and end spec. necessary:

booleanResult = !t.test(/[^\da-z]/i)

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.