javascriptregexregex-greedyword-boundaries

Regex pattern to find pec+


I am trying to match the "word" pec+ in the phrase

I cannot find pec+

I have tried the pattern \bpec\+\b, but there are no matches.


Solution

  • Because + is not a word character, you need to end the expression by matching a non word boundary, using \B:

    \bpec\+\B
    

    See live demo, showing match for

    foo pec+ bar
    

    but not for any of:

    foopec+ bar
    foo pec+bar
    foopec+bar