Is it possible to replace all occurrences except the first one?
So 123.45.67..89.0
should become 123.4567890
.
Edit: I'm looking for a regex. I know how to do it with concat or using the index.
You could use a positive lookbehind to achieve this:
(?<=\..*)\.
So your code would be
"123.45.67..89.0".replace(/(?<=\..*)\./g, '');
Using JS:
var str = "123.45.67.89.0";
var firstOccuranceIndex = str.search(/\./) + 1; // Index of first occurance of (.)
var resultStr = str.substr(0, firstOccuranceIndex) + str.slice(firstOccuranceIndex).replace(/\./g, ''); // Splitting into two string and replacing all the dots (.'s) in the second string
console.log(resultStr);
Hope this helps :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With