Tengo una expresión regular:
var regex = /(layout)(^|\s*)((?<!=)=(?!=))(^|\s*)/;Esto funciona en Google Chrome pero no en Safari ni en Internet Explorer. En Safari, el error es:
"Invalid regular expression: invalid group specifier name"
¿Cómo puedo arreglar esto, por favor?
ACTUALIZAR Tengo una entrada xml en la que ejecuto algunas comprobaciones antes de dársela al analizador xml. Una de las comprobaciones es que uso el análisis de cadenas para extraer el nombre del diseño en los nombres de los archivos incluidos.
Intento identificar espacios en blanco en el atributo 'diseño' y limpiarlo, por ejemplo
<Container width="match_parent" height="wrap_content"> <include width="20px" height="30px" layout = "subcontent.xml" /> </Container>sería cambiado a
<Container width="match_parent" height="wrap_content"> <include width="20px" height="30px" layout="subcontent.xml" /> </Container>Hago esto usando un String.replace, luego extraigo el nombre del archivo incluido y lo cargo desde el almacenamiento y lo pongo en cola para analizarlo también.
Entonces piensa:
let check = 'layout='; //change layout[space....]=[space.....] to layout= let regex = /(layout)(^|\s*)((?<!=)=(?!=))(^|\s*)/; xml = xml.replace(regex, check);Espero que sea más claro ahora.
Usar
let check = 'layout='; let xml = "change layout = "; let regex = /layout\s*=(?!=)\s*/g; xml = xml.replace(regex, check); console.log(xml)Ver prueba de expresiones regulares .
EXPLICACIÓN
"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)