[英]Javascript Regular Expression Match
Try 尝试
<script type="text/javascript">
var str=">1 people>9 people>1u people";
document.write(str.match(/>.*people/img).length);
</script>
at http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_regexp_dot . 在http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_regexp_dot 。 This code should return an array of size 3 but it return array of size 1 . 此代码应返回大小为3的数组,但它返回大小为1的数组。
Where is the problem? 问题出在哪儿?
The .*
part of your regexp is being "greedy" and taking as many characters as it can, in this case returning the entire string as a single match. 正则表达式的.*
部分正在“贪婪”并尽可能多地使用字符,在这种情况下,将整个字符串作为单个匹配返回。
Write it like this instead, with a trailing ?
把它写成这样,带尾随?
: :
str.match(/>.*?people/img)
See the section describing "?" 请参阅描述“?”的部分 in the Mozilla Developer Network JS Reference . 在Mozilla Developer Network JS Reference中 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.