RegExp questions

Let’s say I want to highlight the word “the” in the sentence: They went to the mall.

I would use the RegExp:

   var find_the = /the/g;

However when I use this it highlights both the (‘the’ in they) and the word ‘the’. So how would I form a RegExp that would only find the word “the” and not the letters in order “the”?

\b matches word boundaries

1 Like

Ok thanks! I will try that! :wink:

Okay, so I did what you said just put: \b after the word, but now it is highlighting the ’he’ in ‘the’ why is this happening?

I didn’t say what you should do, and I don’t know what pattern you’re using or what text you’re looking for matches in :confused:

Oh, sorry! :slight_smile: Here are the patterns I useing:

var articles = /the\b|a\b|an\b/g;
	    
var prepositions = /at\b|by\b|during\b|for\b|from\b|of\b|through\b|to\b|with\b/g;
	    
var personal_pronouns = /I\b|we\b|me\b|us\b|you\b|he\b|they\b|him\b|them\b|she\b|her\b|it\b/g;

If you look at your patterns, you’ve got h, e, word-boundary which matches he in the since that has h, then e, and then the end of a word