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

210
Views
JS regexp positive lookbehind no match

Here's the string and the sentence I want to match (extract).

let pigs = "\X The animal 0000 I really dig \X Above all others is the pig. 222 \X Pigs are noble. Pigs are clever 3333, \X Pigs are 5555 courteous.\X However, Now and then, to break this 6666 rule, \X One meets 7777 a pig who is a fool. \X"

" Pigs are 5555 courteous."

Here's two versions of my code. When I check in on various regexp-check websites, it gives the desired match. But when I run in the browser console, it gives null. I have the latest Chrome version. Why doesn't the browser console output the match here?

pigs.match(/(?<=\\X)[^\\X]*5555[^\\X]*(?=\\X)/g);
pigs.match(/(?:\\X)[^\\X]*5555[^\\X]*(?:\\X)/g); 
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could use following pattern:

(?<=\X)(?:.(?!\X))+5555.+?(?=\X)

What I changed is: (?:.(?!\X))+ - which is non-capturing group and expression matches one or more characters, which are not followed by \X.

Also, you wrongly used [^\\X] - it's just negated class, it will match any character other than \ or X.

Below JS example:

let pigs = "\X The animal 0000 I really dig \X Above all others is the pig. 222 \X Pigs are noble. Pigs are clever 3333, \X Pigs are 5555 courteous.\X However, Now and then, to break this 6666 rule, \X One meets 7777 a pig who is a fool. \X"

let match = pigs.match(/(?<=\X)(?:.(?!\X))+5555.+?(?=\X)/g)

console.log(match)

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!