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