javascript - Change only the number inside var? -


if have:

   var = $('.element').text();     // = 'the tortoise ran 5 times faster fox.'; 

is there way can edit change "5" different number in new var? get

newsomething = 'the tortoise ran 6 times faster fox.'; 

note: number var dynamic , 0 -> 100's, need increment 1 , need increment on fly (i cannot assign number var coming document load , being assigned )

var = $('.element').text(); var newsomething = something.replace(/\d+/, function(match) {     return (number(match) + 1).tostring(); }); 

some info on what's happening: code uses more complex form of str.replace function. normal form takes 2 strings, , replaces occurrence of first string second string.

str.replace can take regular expression search (in case, \d+, means "one or more digits"), along replacer function, i've used here. i've used anonymous function expression, takes single argument match. function called every single match, if string has multiple matches, it'll run multiple times.

in function, match (which string) being converted number, incremented, , converted string. returned result of match replacer.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -