jQuery全局正则表达式,用于替换变量 [英] jQuery global regex for replace with variable
本文介绍了jQuery全局正则表达式,用于替换变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将newname
变量的所有实例替换为-
,
但是在我的示例中,newname
是作为文本而不是作为变量来处理的.
in the following example I am trying to replace all instances of the newname
variable with -
,
however newname
in my example is handled as text, rather than a as variable.
var newname = 'test';
var lastname = $(this).attr('name').replace(/newname/g, "-");
有人可以帮忙吗?
推荐答案
var newname = 'test';
var regex = new RegExp(newname,"g")
var lastname = $(this).attr('name').replace(regex, "-");
更多信息:
http://smyck.net/2006/08/11 /javascript-dynamic-regular-expresions/ http://fyneworks.blogspot.com/2007/04/dynamic -regular-expressions-in.html
这篇关于jQuery全局正则表达式,用于替换变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文