JavaScript字符串替换 - 如何在替换字符串中使用匹配变量? [英] JavaScript String replace - How do you use matched variables in the replacement string?

查看:137
本文介绍了JavaScript字符串替换 - 如何在替换字符串中使用匹配变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在替换字符串中使用模式中的匹配变量?

How do you use the matched variables in the pattern in the replacement string?

var regexp = new RegExp('needle', 'ig');
str.replace(regexp, '<span class="marked">//1</span>')


推荐答案

尝试

var regexp = new RegExp(something, 'ig');
str.replace(regexp, '<span class="marked">$&</span>')

参考文献:

如何举例说明将两个单词切换为字符串

这篇关于JavaScript字符串替换 - 如何在替换字符串中使用匹配变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆