Reputation: 323
I want a regex for using in javascript to check if "is" keyword exists
is, a, line // true
this, is, a, line // true
this, is // true
this, is a, line //false
Upvotes: 0
Views: 44
Reputation:
Split on commas and find the is
in the resulting array:
function has_is(str) {
return str.split(/,\s*/) . indexOf('is') !== -1;
}
has_is("is, a, line") // true
has_is("this, is, a, line") // true
has_is("this, is") // true
has_is("this, is a, line") //false
Upvotes: 1
Reputation: 174706
Make sure that the sub-string is
preceeded by start or the line boundary or comma and followed by end of the line boundary or comma with zero or more spaces in-between.
string.match(/(?:^|,)\s*is\s*(?:,|$)/)
Upvotes: 1