我有一個正則運算式:
var regex = /(layout)(^|\s*)((?<!=)=(?!=))(^|\s*)/;
這適用于 Google Chrome,但不適用于 Safari,也不適用于 Internet Explorer。在 Safari 上,錯誤是:
"Invalid regular expression: invalid group specifier name"
請問我該如何解決這個問題?
更新 我有一個 xml 輸入,我在將其提供給 xml 決議器之前對其進行了一些檢查。其中一項檢查是我使用字串決議來提取包含檔案名的布局名稱。
我嘗試識別“布局”屬性上的空格并清理它,例如
<Container
width="match_parent"
height="wrap_content">
<include
width="20px"
height="30px"
layout = "subcontent.xml"
/>
would be changed to
</Container>
將更改為
<Container
width="match_parent"
height="wrap_content">
<include
width="20px"
height="30px"
layout="subcontent.xml"
/>
</Container>
我使用 String.replace 執行此操作,然后提取包含的檔案名并從存盤中加載它并將其排隊以進行決議。
所以想:
let check = 'layout=';
//change layout[space....]=[space.....] to layout=
let regex = /(layout)(^|\s*)((?<!=)=(?!=))(^|\s*)/;
xml = xml.replace(regex, check);
我希望它現在更清楚。
uj5u.com熱心網友回復:
采用
let check = 'layout=';
let xml = "change layout = ";
let regex = /layout\s*=(?!=)\s*/g;
xml = xml.replace(regex, check);
console.log(xml)
請參閱正則運算式證明。
解釋
"layout" - matches the characters layout literally (case sensitive)
"\s*" matches any whitespace character between zero and unlimited times, as many times as possible, giving back as needed (greedy)
"=" - matches the character = with index 6110 (3D16 or 758) literally
- Negative Lookahead "(?!=)":
Assert that the Regex below does not match
"=" - matches the character = with index 6110 (3D16 or 758) literally
"\s*" - matches any whitespace character between zero and unlimited times, as many times as possible, giving back as needed (greedy)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/458789.html
