这种正则表达在Safari上破裂,但在Google Chrome中起作用,我如何使其在这两个方面起作用
我有一条正则是:
var regex = /(layout)(^|\s*)((?<!=)=(?!=))(^|\s*)/;
这在Google Chrome上工作,但在Safari上不行,也不在Internet Explorer上使用。 在Safari上,错误是:
“无效的正则表达式:无效的组规范名称”
我该如何解决此问题?
更新 我有一个XML输入,然后在将其提供给XML解析器之前,在该输入上进行了一些检查。检查之一是我使用字符串解析来提取随附的文件名称上的布局名称。
我尝试在“布局”属性上识别空格,并清理它,例如,
<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>
我使用字符串。重新定位,然后我提取随附的文件名并将其从存储中加载并排队以解析。
所以想一想:
let check = 'layout=';
//change layout[space....]=[space.....] to layout=
let regex = /(layout)(^|\s*)((?<!=)=(?!=))(^|\s*)/;
xml = xml.replace(regex, check);
我希望现在更清楚。
I have a regex:
var regex = /(layout)(^|\s*)((?<!=)=(?!=))(^|\s*)/;
This works on Google Chrome but not on Safari and neither on Internet Explorer.
On Safari, the error is:
"Invalid regular expression: invalid group specifier name"
How can I fix this, please?
UPDATE
I have an xml input on which I run some checks before giving it to the xml parser. One of the checks is that I use string parsing to extract the layout name on included file names.
I try to identify whitespaces on the 'layout' attribute and clean it up e.g.
<Container
width="match_parent"
height="wrap_content">
<include
width="20px"
height="30px"
layout = "subcontent.xml"
/>
</Container>
would be changed to
<Container
width="match_parent"
height="wrap_content">
<include
width="20px"
height="30px"
layout="subcontent.xml"
/>
</Container>
I do this using a String.replace, then I extract the included file name and load it from storage and queue it for parsing also.
So think:
let check = 'layout=';
//change layout[space....]=[space.....] to layout=
let regex = /(layout)(^|\s*)((?<!=)=(?!=))(^|\s*)/;
xml = xml.replace(regex, check);
I hope its clearer now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
参见 Regex Prace 。
解释
Use
See regex proof.
EXPLANATION