I have a situation where I need to test the string if it contains a particular word or letter using the Javascript Regex.
Sample strings would be:
// In the first 3 strings, I need "C" letter to be checked in the string
C is language is required.
We need a C language dev.
Looking for a dev who knows C!
// Keyword is Artificial Intelligence
We need looking for someone who knows Artificial Intelligence.
For checking the above I have created a Regex.
['C', 'Artificial Intelligence', 'D', 'Angular', 'JS'].forEach((item) => {
const baseRex = /[!,.?": ]?/g;
const finalRex = new RegExp(baseRex.source + item + baseRex.source); // /[!,.?": ]<C/D/Angular...>[!,.?": ]/
// Say checking for first iteration only. So let consider 'C'.
console.log(finalRex.test('C is required')); // true
console.log(finalRex.test('Looking for a dev who knows C!')); // true
console.log(finalRex.test('We need a C language dev.')); // true
console.log(finalRex.test('Computer needed')); // Also returns true | Which is wrong!
});
I won’t want the words contains the letter C also get a count.
2
Answers
The regex after the concatenation with the
baseRex
is:Notice that
[!,.?": ]?
can match 0 or 1 characters. InComputer
, both subpatterns of[!,.?": ]?
matches 0 characters, andC
matchesC
, causing the whole regex to match.Presumably, you added
?
there so that it works at the start and end of the string, where there are no characters to be matched. However, you should instead use^
and$
for the start and end instead. Your whole regex should be:You can also replace the character class with
W
, which means[^0-9a-zA-Z_]
.In fact, you don’t actually need to do all of this! There is a useful 0-width matcher called “word-boundary”
b
, which seems to be exactly the thing you want here. Your base regex can just be:It only matches the boundary between a
w
and aW
or between aW
and aw
.for
C
input:
(?<!w)C(?!w)
look behind
extended to both
C
orArtificial Intelligence
input:
(?<!w)((C)|(Artificial Intelligence))(?!w)
Note
for more about
look ahead
andlook behind
, can refer my summary:and my (Chinese) tutorial: 环视断言 · 应用广泛的超强搜索:正则表达式
and even all regex: 一图让你看懂和记住所有正则表达式规则