JavaScript 字符串替换 - 如何在替换字符串中使用匹配的变量? [英] JavaScript String replace - How do you use matched variables in the replacement string?
本文介绍了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屋!
查看全文