javascript - 无法使用 RegExp 替换
问题描述
我正在尝试使用 RegExp 替换一组特定的字符串,但它没有替换。我正在尝试的正则表达式是
\@223(?:\D|'')\gm
要测试的字符串集是这些
@223 ->Replace 223 with #
@223+@33 ->Replace 223 with #
@22; ->Not Replace
@2234 ->Not Replace
@22234 ->Not Replace
@223@44 ->Replace 223 with #
解决方案
如果这是你去的:
var string = `
@223
@223+@33
@22;
@2234
@22234
@223@44
`;
regex = /(?<=@)(223)(?=\D)/g;
string = string.replace(regex, "#");
console.log(string);
输出 :
@#
@#+@33
@22;
@2234
@22234
@#@44
解释 :
(?<=@) : test if leaded by @ character.
(?=\D) : followed by any character except digit
推荐阅读
- reactjs - 使用 alert 打印文本以外的其他内容
- visual-studio - Intellisense 在 Visual Studio for Sql Server 数据项目中不起作用
- python - ImportError:无法从部分初始化的模块“dogehouse”导入名称“DogeClient”(很可能是由于循环导入)
- c# - 逐行发送,不重复
- python - Windows OS下Nodejs运行python脚本
- javascript - 如何将 MapQuest RouteLayer 添加到传单图层
- apache-camel - 我想通过camel-context.xml配置2个消息队列,所以我需要基于条件的路由
- java - Java:向while循环添加用户输入
- azure - 禁用 Azure Active Directory 单一注销
- reactjs - 从输入中下载带有 URL 的图像并将其发布以使用其他字段表示后端?