Quiero validar cierto elemento en xml usando una expresión regular.
<ConfigOption>value</ConfigOption>Requisitos para ConfigOption:
Esto es lo que tengo.
(^[a-zA-Z0-9 _]*$)|(?:\??config=\d)|(^CONFIG\-\d$)por ejemplo: 'hola' debería estar bien. 'hello?config=20' debería estar bien 'CONFIG-20' debería estar bien. 'hola-' debería fallar 'hola*' debería fallar
¿Alguien puede arreglar mi expresión regular? cuando pruebo 'hello=' o 'hello*' 'https://regex101.com/', todavía coincide.
Aquí hay una solución que creo que coincide con sus requisitos. Avíseme si necesita refinación.
La verdadera diferencia está en agregar la búsqueda anticipada (?=\W) .
Tenga en cuenta que la tercera condición es redundante e innecesaria.
const regex = /(?=\W)(^\b[a-zA-Z0-9 _]*)|(^[a-zA-Z0-9 _]*(?:\??[Cc][Oo][Nn][Ff][Ii][Gg][=,-]\d{2}))/; const tests = ['*HELLO', 'hello', 'hello=', 'hello*', 'hello?CONFIG-20', 'hello?CONFIG=20', 'config=20']; for (const test of tests) { console.log(regex.test(test)); }