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

203
Views
How to convert this Regex with lookbehind so it works in Safari?

This Regex works fine in Chrome and Firefox

let regexp = new RegExp(`^${searchTerm}|(?<=\\s)${searchTerm}`, 'gi')

but unfortunately Safari complains

SyntaxError: Invalid regular expression: invalid group specifier name

It looks like Safari doesn't support the lookbehind, but how can I transform it so I get the indices of the searchTerm without the whitespaces?

let regexp = new RegExp(`^${searchTerm}|(?<=\\s)${searchTerm}`, 'gi')
let matchIndices = [...string.matchAll(regexp)].map(match => match.index);

matchIndices.forEach(index => {
   ...
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use a regex like /(^|\s)word/gi and if Group 1 does not match a whitespace, collect the match index, otherwise, match index + 1 value:

let searchTerm = "foo"
let string = "foo fooo,foo"

let regexp = new RegExp(`(^|\\s)${searchTerm}`, 'gi')
var matchIndices = [], m;
while(m = regexp.exec(string)) {
  if (m[1] === "") {
    matchIndices.push(m.index)
  } else {
    matchIndices.push(m.index+1)
  }
}

matchIndices.forEach(index => {
   console.log(index)
});

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!