jquery - Regex replace doesn't replace the matched numbers -
i'm trying replace attribute values using regex check when try replace numbers doesn't work. when check console can see numbers getting changed correctly:
["5, 6"] //this after initial matching [4, 5] //this result after i've done converting , subtracting
in for loop
convert items in array integers first , subtract each 1.
button.on('click', function() { var btnparent = $(this).parent().parent(); btnparent.fadeout(400, function() { btnparent.remove(); }); btnparent.nextall('li').find('input, label').each(function () { var self = $(this); $.each(this.attributes, function(i, attrib){ var attribvalue = attrib.value; var attribname = attrib.name; var intregex = /\d+/g; var originalnum = attribvalue.match(intregex); var newnum = originalnum; var newvalue; if (newnum != null) { (j = 0; j < newnum.length; j++) { parseint(newnum[j]); newnum[j]--; newvalue = attribvalue.replace(originalnum[j], newnum[j]); //this doesn't work } } self.attr('' + attribname, newvalue); }); });
console output:
the original value is: text5 (type = string) original num is: 5 (type = object) new num is: 4 (type = number) new value is: text5 (type = string)
despite working can't replace numbers, suspect might have types, can't change original number when it's object or string , new num number or that. i've tried change correspond either nan or no difference in output. wrong?
you're using attrib.name
, attrib.value
, not checking if name
attribute or not. better approach following
$('[name]').each(function() { $(this).attr("name", reduceby1); $(this).attr("id", reduceby1); }); function reduceby1(_, n){ return n.replace(/\d+/g, function(m){ return --m }); }
Comments
Post a Comment