admin管理员组

文章数量:1287858

I have small requirement.I want to search a string with exact match. Suppose i want to search for None_1, i am searching for 'None_1' using /None_1/, but it is matching even "xxxNone" but my requirement is it should match only None_[any digit]. Here is my code

/^None_+[0-9]{?}/

So it should match only None_1 , None_2

I have small requirement.I want to search a string with exact match. Suppose i want to search for None_1, i am searching for 'None_1' using /None_1/, but it is matching even "xxxNone" but my requirement is it should match only None_[any digit]. Here is my code

/^None_+[0-9]{?}/

So it should match only None_1 , None_2

Share Improve this question asked Jun 27, 2011 at 9:03 ReddyReddy 1,3853 gold badges23 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You should also anchor the expression at the end of the line. But that alone will not make it work. Your expression is wrong. I think it should be:

/^None_[0-9]+$/
  • ^ matches the beginning of a line
  • [0-9]+ matches one or more digits
  • None_ matches None_
  • $ matches the end of a line

If you only want to match one digit, remove the +.


Your original expression /^None_+[0-9]{?}/ worked like this:

  • ^ matches the beginning of a line
  • None matches None
  • _+ matches one or more underscores
  • [0-9] matches one digit
  • {? matches an optional opening bracket {
  • } matches }

Try this:

/^None_+[0-9]{?}$/

本文标签: javascriptHow to match with an exact string using regular expressionStack Overflow