I want to validate a certain element in xml by using regular expression.
<ConfigOption>value</ConfigOption>
Requirements for ConfigOption:
Here is what I have.
(^[a-zA-Z0-9 _]*$)|(?:\??config=\d)|(^CONFIG\-\d$)
for example: 'hello' should be ok. 'hello?config=20' should be ok 'CONFIG-20' should be ok. 'hello-' should fail 'hello*' should fail
can someone fix my regular expression? when I test 'hello=' or 'hello*' 'https://regex101.com/', it still matches.
Here is a solution I think matches your requirements. Let me know if it needs refining.
The real difference is in adding the lookahead (?=\W).
Note the third condition is redundant and unneeded.
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));
}