Skip to content Skip to sidebar Skip to footer

Jquery Global Regex For Replace With Variable

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 newna

Solution 1:

var newname = 'test';
var regex = newRegExp(newname,"g")
var lastname = $(this).attr('name').replace(regex, "-");

More info:

http://smyck.net/2006/08/11/javascript-dynamic-regular-expresions/http://fyneworks.blogspot.com/2007/04/dynamic-regular-expressions-in.html

Post a Comment for "Jquery Global Regex For Replace With Variable"