首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JS -用于替换字符串的所有实例的正则表达式(使用变量属性)

JS -用于替换字符串的所有实例的正则表达式(使用变量属性)
EN

Stack Overflow用户
提问于 2020-02-13 10:58:15
回答 1查看 36关注 0票数 1

我正在为CMS创建一个组件,它在文本输入的响应上运行一个查找/替换,它应该用

我已经成功地创建了一个简单的string.replace,它成功地替换了单个实例(我的regex完全匹配),但是,我需要一些帮助来重构重构以找到所有实例。

代码语言:javascript
复制
article.content = "this is an image, {html_image id=222}. This is also an image {html_image id=111}

article.content = article.content.replace(
      new RegExp('{html_image id=222}'),
      '<img src="" />'
    );

current output = "this is an image, <img src="222"/>. This is also an image {html_image id=111}"
expected output = "this is an image, <img src="222"/>. This is also an image <img src="111"/"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-13 11:07:21

您可以使用以下代码(使其适应您的变量):

代码语言:javascript
复制
var content = "this is an image, {html_image id=222}. This is also an image {html_image id=111}";
content = content.replace(
  /{html_image id=(\d*)}/g,
  '<img src="$1" />'
);
console.log(content);

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60206197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档