正则表达式在csv中搜索一个精确的字符串

Regex to seach a exactly string in csv

本文关键字:一个 字符串 csv 搜索 正则表达式      更新时间:2023-09-26

我想要一个正则表达式使用在javascript检查是否"is"关键字存在

is, a, line // true
this, is, a, line // true
this, is // true
this, is a, line //false

确保子字符串is前面是开始或行边界或逗号,后面是行边界的结束或逗号,中间有零个或多个空格。

string.match(/(?:^|,)'s*is's*(?:,|$)/)
演示

分隔逗号,在结果数组中查找is:

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