检测字符串中是否包含引号,并且该引号中包含逗号

时间:2017-10-29 11:06:28

标签: javascript regex

我想检查在引号结束之前,带引号的字符串(用引号""表示)是否包含逗号。

请参阅以下示例:

1)

He said "hi," then left.

2)

He said "hi", then left.

3)

He said "hi, ho", then left.

1)中,第二个引号前面有一个逗号,所以这应该被正则表达式捕获。

2)不应该被抓住。

3)也不应该被抓住。

所以我希望只有在字符串包含引号并且该引号在其结束之前在其中包含逗号时才能获得肯定。除了true或false之外我不需要任何其他内容这个正则表达式的结果。

我很抱歉我没有正则表达式 - 我从来没有使用过这个,只需要一个针对reddit增强套件的单一过滤规则:S

哦,是的,这应该是在javascript正则表达式(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

4 个答案:

答案 0 :(得分:3)

使用像这样的正向前瞻

"[^"]+,(?=")

.test()一起,请参阅a demo on regex101.com

答案 1 :(得分:3)

简单的正则表达式将完成这项工作:

/".*?,"/

测试它:https://regex101.com/r/3cl4h5/1

答案 2 :(得分:0)

String a1 = "He said \"hi,\" then left.";
String a2 = "He said \"hi\", then left.";
String test = ",\"";

if(a2.contains(test))会给你假,if(a1.contains(test))会给你真实...

答案 3 :(得分:0)

不知道该解释什么:



let pattern = /"[^"]*,[^"]*"/g;

let strings = [
  'He said "hi," then left.',
  'He said "hi", then left.',
  'He said "hi, ho", then left.',
  '"He" said "hi," then "left."',
];

strings.forEach(str => {
  console.log(str);
  console.log(str.match(pattern));
})

.as-console-wrapper{top:0;max-height:100%!important}




相关问题