此正则表达式在 safari 上中断但在 Google Chrome 中有效,如何使其在两者中都有效
This regular expression breaks on safari but works in Google Chrome, how do I make it work in both
我有一个正则表达式:
var regex = /(layout)(^|\s*)((?<!=)=(?!=))(^|\s*)/;
这适用于 Google Chrome 但不适用于 Safari 和 Internet Explorer。
在 Safari 上,错误是:
"Invalid regular expression: invalid group specifier name"
请问我该如何解决这个问题?
更新
我有一个 xml 输入,在将它提供给 xml 解析器之前,我对它进行了 运行 一些检查。其中一项检查是我使用字符串解析来提取包含文件名的布局名称。
我尝试识别 'layout' 属性上的空格并清理它,例如
<Container
width="match_parent"
height="wrap_content">
<include
width="20px"
height="30px"
layout = "subcontent.xml"
/>
</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);
我希望现在更清楚了。
使用
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)
我有一个正则表达式:
var regex = /(layout)(^|\s*)((?<!=)=(?!=))(^|\s*)/;
这适用于 Google Chrome 但不适用于 Safari 和 Internet Explorer。 在 Safari 上,错误是:
"Invalid regular expression: invalid group specifier name"
请问我该如何解决这个问题?
更新 我有一个 xml 输入,在将它提供给 xml 解析器之前,我对它进行了 运行 一些检查。其中一项检查是我使用字符串解析来提取包含文件名的布局名称。
我尝试识别 'layout' 属性上的空格并清理它,例如
<Container
width="match_parent"
height="wrap_content">
<include
width="20px"
height="30px"
layout = "subcontent.xml"
/>
</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);
我希望现在更清楚了。
使用
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)