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"
/>
would be changed to
</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.
CodePudding user response:
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)