这种正则表达在Safari上破裂,但在Google Chrome中起作用,我如何使其在这两个方面起作用

发布于 2025-01-22 08:20:48 字数 1104 浏览 4 评论 0原文

我有一条正则是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆啦不做梦 2025-01-29 08:20:48

使用

let check = 'layout=';
let xml = "change layout    =   ";
let regex = /layout\s*=(?!=)\s*/g;
xml = xml.replace(regex, check);
console.log(xml)

参见 Regex Prace

解释

"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)

Use

let check = 'layout=';
let xml = "change layout    =   ";
let regex = /layout\s*=(?!=)\s*/g;
xml = xml.replace(regex, check);
console.log(xml)

See regex proof.

EXPLANATION

"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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文