Reputation: 270
i want to check anyone not enter yahoo,gmail or any group word.for that i have create regular expression as below but it gives invalid group when i try to put in javascript.
if (str.match('/^(.*?(\W|^)(?i)(yahoo|gmail|com)(\W|$))$/')) {
return true;
}
Upvotes: 2
Views: 1625
Reputation: 626690
The error you get is related to (?i)
, an inline modifier that is not supported by JS regex engine. (\W|^)
is a common construct in some libraries to denote a word boundary, but in JS, here, it is easier to use a word boundary instead, \b
.
I suggest
var str = "YaHoo here";
if (/\b(?:yahoo|gmail|com)\b/i.test(str)) {
console.log("Contains forbidden word!");
} else {
console.log("Good!");
}
Pattern details:
\b
- a leading word boundary(?:yahoo|gmail|com)
- any of the 3 alternatives\b
- trailing word boundaryUpvotes: 4