I have a program where the user will type words into an input box and the computer will check the input for key words. I have a slight problem, see below:
- User input #1: "Perfect"
- User input #2: "Perfect how are you?"
I created the regex /(\bperfect\b)/ig.
How can I modify my regular expression so that it will only match the word "perfect" by itself and not perfect in user input #2?
>Solution :
This regex should work:
/^Perfect$/g
The anchor ^ matches the start of the string and the anchor $ matches the end of the string. More on this here!