0

这是一个支持非拉丁字母的单词边界正则表达式,它在 Chrome 中有效,但在 Safari 中无效。任何替代解决方案?

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

谢谢

4

1 回答 1

1

Safari 不支持 Look behind 断言。在此处检查兼容性表
您可以尝试不使用它。

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

例子

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

于 2020-09-03T08:04:26.903 回答