admin管理员组文章数量:1327126
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, "-");
Can anyone help out?
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, "-");
Can anyone help out?
Share Improve this question asked Oct 25, 2011 at 16:12 baikbaik 1,0132 gold badges15 silver badges21 bronze badges1 Answer
Reset to default 9var newname = 'test';
var regex = new RegExp(newname,"g")
var lastname = $(this).attr('name').replace(regex, "-");
More info:
http://smyck/2006/08/11/javascript-dynamic-regular-expresions/ http://fyneworks.blogspot./2007/04/dynamic-regular-expressions-in.html
本文标签: javascriptjQuery global regex for replace with variableStack Overflow
版权声明:本文标题:javascript - jQuery global regex for replace with variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742200275a2431784.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论