简体   繁体   中英

regex match entire word ( characters between whitespace ) that contain letters in this order

I'm trying to match an entire words, characters between whitespace, if it contains the letters PRP in that case and in that order in javascript.

What I'm trying to do is select all words that contain PRP and replace them with a ( space ).

Here is a regex that I could come up with that matches anything with the letters p or r or p: \\b(?=\\w*[PRP])\\w+\\b .

once I know the regex my plan is to do str.replace(matchAllWordsWithregex, ' ');

So what is the regex and how do I say all words with this regex in the replace function?

You need to remove the square brackets around PRP if you want the literal text PRP to match (because with them, you're creating a character class that matches either a P or an R ):

\b(?=\w*PRP)\w+\b

But you don't actually need a lookahead assertion (nor word boundaries) here:

\w*PRP\w*

will work just fine (for alphanumeric "words"). To replace all matches in a string subject , you can use

result = subject.replace(/\w*PRP\w*/g, " ");

If you define a word as "anything except whitespace", use

result = subject.replace(/\S*PRP\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