简体   繁体   中英

How to match with an exact string using regular expression

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

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 }

尝试这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM