![](/img/trans.png)
[英]How to get both the start and end positions of regex matches in JavaScript?
[英]Start and end positions of the *groups* of a Javascript stdlib regex match
是否可以獲得 Javascript 正則表達式匹配組的開始和結束位置?
注意:“組”。
(奇怪的是我找不到有關此的信息。這表明它不可用。這很奇怪。Java 和 Python 似乎具有此功能。)
此信息位於RegExp.exec()
(以及定義為返回相同類型結果的其他方法)的返回值的indices
屬性中。 正則表達式必須具有d
標志才能啟用返回它。 編號組在indices
數組中,命名組是索引的屬性。組indices.groups
。
// Match "quick brown" followed by "jumps", ignoring characters in between // Remember "brown" and "jumps" // Ignore case const re = /quick\s(?<color>brown).+?(jumps)/igd; const result = re.exec('The Quick Brown Fox Jumps Over The Lazy Dog'); console.log(result.indices); console.log(result.indices.groups);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.