duy.ly
duy.ly

Reputation: 323

Regex to seach a exactly string in csv

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

Answers (2)

user663031
user663031

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

Avinash Raj
Avinash Raj

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*(?:,|$)/)

DEMO

Upvotes: 1

Related Questions