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

252
Views
Regex: Match every character between two strings

I am trying to find a way to match every character between two strings.

For example, for the given string abc--def--ghi, I want the regex to match d, e, and f.

I've tried using the following regex (?<=--)(.*)(?=--), however this matches all the characters between -- (def), whereas I need to match every character between --.

s.match(/--(.*?)--/)[1].split("") doesn't work as I need to do this without splitting.

Any suggestions?

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

0

In JavaScript, using the ECMAScript 2018+ compliant regex engine, you can achieve what you want without additional split step using

/(?<=--(?:(?!--).)*).(?=(?:(?!--).)*--)/gs
/(?<=--[^-]*(?:-(?!-)[^-]*)*).(?=[^-]*(?:-(?!-)[^-]*)*--)/gs

See the regex demo (the second variant is the same regex as the first one, but more efficient as it follows the "unroll-the-loop" principle). Details:

  • (?<=--(?:(?!--).)*) - a location immediately preceded by -- and then any one or more (as many as possible) chars, each of which does not start a -- char sequence
  • . - any single char
  • (?=(?:(?!--).)*--) - immediately followed by any one or more (as many as possible) chars, each of which does not start a -- char sequence, and then --.

The s flag enables . to match any char including line break chars that . does not match by default.

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!