3

How to match specific words that are not part of a word?

For example, I'd like the regex to detect the three "on" but not the one in "long".

on long on abc the on

When I use (?: |^)the|on|of(?: |$), it detects the "on" in "long" as well which is not what I am looking for.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Ted
  • 55
  • 2
  • 11

2 Answers2

4

There are two things here to mention:

  • When you want to restrict a context for a group of words, always group them
  • To match as a whole word, you need to use word boundaries, \b

So, you need

\b(?:the|on|of)\b

See this regex demo.

enter image description here

You can see on the diagram that the word boundaries now pertain to all the alternatives listed with | operator due to the fact they are all enclosed with a non-capturing group.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

You can use this regex :

/\bon\b/

The \b tokens are matching any word boundaries, would they be spaces, string start or end, etc...

So this is pretty straight forward : you need the word on, surrounded by word boundaries.

Seblor
  • 6,947
  • 1
  • 25
  • 46