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

查看:21
本文介绍了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>')

推荐答案

try

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

参考文献:

关于如何切换两个的示例单词转换成字符串

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

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