[英]What's the problem with my script?
Why is this giving me null
? 为什么这使我
null
?
<script language="javascript">
var element = "11";
var string = "7,11";
var check = string.match("/(^|\D)"+element+"(\D|$)/g");
alert(check);
</script>
When I run the regex
on http://regex.larsolavtorvik.com/ it works correctly. 当我在http://regex.larsolavtorvik.com/上运行
regex
,它可以正常工作。
Pleeeease help I want to sleeeep! 求助,我想睡觉! :))
:))
var check = string.match(new RegExp("(^|\\D)"+element+"(\\D|$)", "g"));
You have to escape the \\
in the string literal, or the regex engine will see \\D
as D
. 您必须在字符串文字中转义
\\
,否则正则表达式引擎会将\\D
视为D
EDIT: Sorry, I should have read more carefully. 编辑:对不起,我应该仔细阅读。 You don't want the
/
es, and you need to handle /g
separately. 您不需要
/
es,并且需要单独处理/g
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.