最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why this Regex, matches incorrect characters? - Stack Overflow

programmeradmin3浏览0评论

I need to match these characters. This quote is from an API documentation (external to our pany):

Valid characters: 0-9 A-Z a-z & # - . , ( ) / : ; ' @ "

I used this Regex to match characters:

^[0-9a-z&#-\.,()/:;'""@]*$

However, this wrongly matches characters like %, $, and many other characters. What's wrong?

You can test this regular expression online using /, and this regular expression is meant to work in both .NET and JavaScript.

I need to match these characters. This quote is from an API documentation (external to our pany):

Valid characters: 0-9 A-Z a-z & # - . , ( ) / : ; ' @ "

I used this Regex to match characters:

^[0-9a-z&#-\.,()/:;'""@]*$

However, this wrongly matches characters like %, $, and many other characters. What's wrong?

You can test this regular expression online using http://regexhero/tester/, and this regular expression is meant to work in both .NET and JavaScript.

Share Improve this question asked Oct 8, 2011 at 13:22 Saeed NeamatiSaeed Neamati 35.9k41 gold badges139 silver badges192 bronze badges 1
  • dont suppose escaping the extra hyphen in there (or moving it to the beginning or end) would change anything. – machinaut Commented Oct 8, 2011 at 13:42
Add a ment  | 

5 Answers 5

Reset to default 6

You are not escaping the dash -, which is a reserved character. If you add replace the dash with \- then the regex no longer matches those characters between # and \

Move the literal - to the front of the character set:

^[-0-9a-z&#\.,()/:;'""@]*$

otherwise it is taken as specifying a range like when you use it in 0-9.

- sign, when not escaped, has special meaning in square brackets. #-\. is transformed into #-. (BTW, backslash before dot is not necessary in square brackets), which means "any character between # (ASCII 0x23) and . (ASCII 0x2E). The correct notation is

^[0-9a-z&#\-.,()/:;'"@]*$

The special characters in a character class are the closing bracket (]), the backslash (\), the caret (^) and the hyphen (-).

As such, you should either escape them with a backslash (\), or put them in a position where there is no ambiguity and they do not need escaping. In the case of a hyphen, this would be the first or last position.

You also do not need to escape the dot (.).

Your regex thus bees:

^[-0-9a-z&#.,()/:;'"@]*$


As a side note, there are many available regex evaluators which provide code hinting. This way, you can simply hover your mouse over your regular expression and it can be explained in English words. One such free one is RegExr.

Typing your original regex in it and hovering over the hyphen shows:
Matches characters in the range '#-\'

Try that

^[0-9a-zA-Z\&\#\-\.\,\(\)\/\:\;\'\"\@]*$
发布评论

评论列表(0)

  1. 暂无评论