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 => {
   ...
});

Source: JavaSript – Stack Overflow

November 26, 2021
Category : News
Tags: javascript | regex

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.