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

266
Views
Regex: Equivalent expression without using positive lookbehind

I need to split 4d6/2d6/1d6 matching /. However, in order to split it I need to have specifically \d preceding / and another \d|d succeeding /, so other expressions like 1d6+db/2 don't get split. I've come to the solution of using positive lookbehind, but I need my regex to be valid in IOS systems too. The solution I've come up with is /(?<=\d)\/(?=\d|d)/g. Is there a way to create an equivalent to this expression without using positive lookbehind?

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

0

You can use an extracting, matching approach rather than splitting:

/(?:\/|^)(.*?\d)(?=\/\d|$)/g
/(?:\/|^)([\w\W]*?\d)(?=\/\d|$)/g

See the regex demo. Your value is in Group 1. The regex with [\w\W] matches across lines, too (as . does not match line breaks by default).

Details:

  • (?:\/|^) - a non-capturing group that matches either / or start of string
  • (.*?\d) - Group 1: any zero or more chars (here, other than line break chars) as few as possible, and then a digit
  • (?=\/\d|$) - a location that is immediately followed with / + digit, or end of string.

In JavaScript, you can either use const matches = Array.from(text.matchAll(/(?:\/|^)(.*?\d)(?=\/\d|$)/g), x => x[1]), or - if you cannot use this syntax - a more verbose legacy extraction like

var s = "4d6/2d6/1d6";
var re = /(?:\/|^)(.*?\d)(?=\/\d|$)/g;
var matches=[], m;
while(m=re.exec(s)) {
  matches.push(m[1]);
}
console.log(matches)

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!