[英]Javascript Regular Expression Match
嘗試
<script type="text/javascript">
var str=">1 people>9 people>1u people";
document.write(str.match(/>.*people/img).length);
</script>
在http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_regexp_dot 。 此代碼應返回大小為3的數組,但它返回大小為1的數組。
問題出在哪兒?
正則表達式的.*
部分正在“貪婪”並盡可能多地使用字符,在這種情況下,將整個字符串作為單個匹配返回。
把它寫成這樣,帶尾隨?
:
str.match(/>.*?people/img)
請參閱描述“?”的部分 在Mozilla Developer Network JS Reference中 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.