Javascript Replace, Ignore The First Match
I have the following script in javascript var idText; var idText1; idText = 'This_is_a_test'; idText1 = idText.replace(/_/g, ' '); alert(idText1); When I show idText1, it replaces
Solution 1:
It is certainly possible, here is one option:
var n = 0;
idText1 = idText.replace(/_/g, function($0) {
n += 1;
return n === 1 ? $0 : " ";
});
This uses a callback for the replacement that increments a counter for each match, and replaces the first match with the original text by checking the value of that counter.
Post a Comment for "Javascript Replace, Ignore The First Match"