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

217
Views
Can regexp avoid a pattern in JavaScript?

I have an expression in JS to match all the words containing (for example) 'foo+', but I would like to discard those of them who end with some other pattern.

Is there a way to make this in only one regular expression?

example:

myExp = /foo+\w*/; /*should add something to avoid ending with 'ba+r'*/

myExp.matchAll('boo, fo, food, foobaaar, foobr');

should result in ["food", "foobr"] not including 'foobaaar'

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

0

You want to use a negative lookahead like (?!not_this), of course replace not_this with the appropriate pattern to exclude using word-break characters \b, or whatever fits your logic.

let myExp = /\bfoo+(?!\w*ba+r\b)\w*/g;

console.log(...' boo fo food foobaaar foobr foobaardd boofoo foodbar '.matchAll(myExp));

about 4 years ago · Juan Pablo Isaza Report

0

You can use negative look ahead. To exclude cases where ba+r is not the ending, use \b in that look ahead.

let myExp = /foo+(?!\w*ba+r\b)\w*/g;
let input = 'boo, fo, food, foobaaar, foobr, foobaardd';

console.log(...input.match(myExp));

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!