简体   繁体   中英

regex prevent match word

i'm using the regex

/part\s?[0-9]*\s?:?/g

to match words like

" part 20 " " part21 " " part30: " etc

the problem is that it matchs also the word "part" inside other words like "Re part ition"

it should match word only if it is a whole word ie

part 01       -match
part 33:      -match
part          -no match
participial   -no match

Seems like what you want is to make one or more digits mandatory, changing * to + , and adding \\b to ensure the "part" is not the end of a larger word:

/\bpart\s?[0-9]+\s?:?/g

And if you are actually using the matched string(s), I would think you would only want to include the whitespace after a number if there was a colon too:

/\bpart\s?[0-9]+(?:\s?:)?/g

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