kranthi p
kranthi p

Reputation: 31

regular expression to match basic ascii characters

I am checking for the regular expression which matches only basic ASCII characters in javascript. I tried the below solutions given in the other threads in the forum.

^[\\u0000-\\u007F]*$
^[\x00-\x7F]*$

However, the above is not working as supposed to be when i gave the English alphabet also. Any suggestions to come up with the regular expression for accepting the basic ASCII characters please ?

Edit: I am trying to validate a text box description field.

Upvotes: 1

Views: 289

Answers (1)

loxxy
loxxy

Reputation: 13151

Seems to be be working fine.

For a string (containing unicode):

"sdfs \u2022"                                 // "sdfs •"

For matching only ASCII part of the string:

"sdfs \u2022".match("[\\u0000-\\u007F]*")     // "sdfs "

But if you need to check that the string is composed of only ASCII:

"sdfs \u2022".match("^[\\u0000-\\u007F]*$")   // null

For a string (not containing unicode):

"sdfs ".match("^[\\u0000-\\u007F]*$")         // "sdfs "

Upvotes: 1

Related Questions