使用正则表达式javascript查找字符串

时间:2016-05-28 17:07:54

标签: javascript regex

我想找到字符串,其最后一个字符可能出现也可能不出现

"abc,efg,ASD"
"abc,ASD,efg"

所以,我必须找到有或没有“,”

的ASD

2 个答案:

答案 0 :(得分:2)

描述

[,"](ASD)[,"]

Regular expression visualization

此正则表达式将执行以下操作:

  • 匹配逗号或逗号和引号之间的asd,
  • 将值放入捕获组1

实施例

现场演示

https://regex101.com/r/mV9vA9/1

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  [,"]                     any character of: ',', '"'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    ASD                      'ASD'
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  [,"]                     any character of: ',', '"'
----------------------------------------------------------------------

答案 1 :(得分:0)

这会回答你的问题吗?



var regex = /,ASD,?/g;
var result = regex.exec("abc,ASD\nabc,ASD,dsa");
console.log(result);