Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
regular expression with 3 conditions with javascript and typescript

I want to validate a certain element in xml by using regular expression.

<ConfigOption>value</ConfigOption>

Requirements for ConfigOption:

  1. characters, numbers, underscore and space are allowed.
  2. CONFIG-XX should be allowed. XX is numbers
  3. CONFIGNAME?config=XX format should be allowed. xx is numbers.

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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));
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!