[英]Javascript regex get matches as array
我有以下代碼片段:
hash = window.location.hash;
regex = /^#id=\d{1,10}&session=(.+)$/ig;
if (regex.test(hash)) {
stepTo4(0, id, "start", session);
break;
}
我該如何在方法參數中當前位於id
的位置以及與session
相同的位置獲取\\d{1,10}
的匹配值?
我試過了:
var m = hash.match(regex);
console.log(m);
stepTo4(0, m[0], "start", m[1]);
但是m[0]
僅包含與hash
變量相同的值,並且m[1]
未定義。
您需要在表達式中捕獲它:
hash = window.location.hash;
regex = /^#id=(\d{1,10})&session=(.+)$/ig;
要使用它:
m = hash.match(regex)
id = m[1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.