我試圖得到一個可選的lookahead,但有問題,只要我讓它可選(在它後面添加一個?
),即使數據存在,它也不再匹配。可選的正則表達式lookahead
作爲一個簡短的總結,我試圖從URI中提取特定查詢字符串參數。例如:
/.*foo.html\??(?=.*foo=([^\&]+))(?=.*bar=([^\&]+))/
.exec('foo.html?foo=true&bar=baz')
我會打破了一點:
.*foo.html\?? // filename == `foo.html` + '?'
(?=.*foo=([^\&]+)) // find "foo=...." parameter, store the value
(?=.*bar=([^\&]+)) // find "bar=...." parameter, store the value
上面的例子,這兩個foo
和bar
存在如在查詢字符串參數的條件下完美。問題是,我試圖使這些可選的,所以我把它改爲:
/.*foo.html\??(?=.*foo=([^\&]+))?(?=.*bar=([^\&]+))?/
↑ ↑
Added these question marks ─┴──────────────────┘
,它不再匹配任何參數,但它仍然匹配foo.html
。有任何想法嗎?
你爲什麼使用正則表達式呢?只需拆分字符串並將鍵/值對存儲在對象中即可。 – ThiefMaster
@ThiefMaster - 因爲我想:)而現在我想更多的是它不會工作。實際上,這僅僅是因爲當我只需要獲得2個參數時代碼更短。 –
爲什麼你需要lookahead?要匹配foo.html?bar = baz&foo = true以及? – Bergi