Skip to content
Advertisement

regex whole string match between numbers

I want to extract a whole word from a sentence. Thanks to this answer,

JavaScript

I can get whole words in cases like:

JavaScript

where symbols next to the word don’t bother.

However if there’s a number it doesn’t find the word.

How should I modify the expression to match cases where there’s a number next to the word? Like:

JavaScript

Advertisement

Answer

Can use the regexp (?:d|b){0}(?:d|b) to match the target word with either a word-boundary or a digit on either side of it.

JavaScript

Output:

JavaScript

If you want to reuse the same target word against multiple inputs or in a loop then you can assign findWholeWord() call to a variable then call it.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement