Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pharse-Javascript中的正则表达式_Javascript_Regex - Fatal编程技术网

pharse-Javascript中的正则表达式

pharse-Javascript中的正则表达式,javascript,regex,Javascript,Regex,我有这样的表达: jQuery("#SomeId") 我正在使用 jQuery\(\".*?\"\) 实际结果: jQuery("#SomeId") 预期结果: #SomeId 如何执行此操作?将正则表达式更改为: jQuery\(\"(.*?)\"\) 和#SomeId将在第1组中。尝试: var str = 'jQuery("#SomeId")'; var reg = /#(\w+)/; var ret = reg.exec(str); // ["#SomeId", "Some

我有这样的表达:

jQuery("#SomeId")
我正在使用

jQuery\(\".*?\"\)
实际结果:

jQuery("#SomeId")
预期结果:

#SomeId
如何执行此操作?

将正则表达式更改为:

jQuery\(\"(.*?)\"\)
#SomeId
将在第1组中。

尝试:

var str = 'jQuery("#SomeId")';

var reg = /#(\w+)/;

var ret = reg.exec(str); // ["#SomeId", "SomeId"]
如果您希望使用
#
获得结果,请使用
ret[0]
,否则
ret[1]

console.log(ret[0]); // #SomeId
console.log(ret[1]); // SomeId