Javascript RegEx safari issue

889 views Asked by At

This is a word boundary Regular Expression which support non latin letters , it works in Chrome but not in Safari. any alternative solution?

new RegExp("(?<=[\\s,.:;\"']|^)" + word + "(?=[\\s,.:;\"']|$)","g");

Thanks

1

There are 1 answers

4
Liju On

Look behind assertions are not supported in Safari. Check compatibility table here
Instead you can try without it.

const regx = new RegExp("([\\s,.:;\"']|^)("+word+")([\\s,.:;\"']|$)","g");

Example

let word="word";
const regx = new RegExp("([\\s,.:;\"']|^)("+word+")([\\s,.:;\"']|$)","g");
console.log('hello word, beautiful word'.replace(regx,'$1World$3'));