【问题标题】:Javascript RegEx safari issueJavascript RegEx Safari 问题
【发布时间】:2020-12-22 06:57:57
【问题描述】:

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

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

谢谢

【问题讨论】:

  • new RegExp("\\b" + word + "\\b","g"); 不工作吗?
  • \b 仅支持拉丁字母(a-z、A-Z、0-9)。我想在印度语言上工作

标签: javascript regex browser safari word-boundaries


【解决方案1】:

Safari 不支持 Look behind 断言。检查兼容性表here
相反,您可以尝试不使用它。

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'));

【讨论】:

  • 它也选择了空白,也想避免第一个空白。
  • @Sarath 我已经用代码更新了答案。请检查。
  • 'hello word, beautifull word'.replace(regx,'World') => "helloWorld beautifullWorld" , i want "hello World beautifull World"
  • @Sarath 我已经更新了上面的答案。请检查。
猜你喜欢
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 2018-01-16
  • 1970-01-01
相关资源
最近更新 更多