How to Replace All Dots in a String Using JavaScript

How to replace all dots in a string using JavaScript

You need to escape the . because it has the meaning of "an arbitrary character" in a regular expression.

mystring = mystring.replace(/\./g,' ')

Replace all the dots in a number

replace returns a string. Try:

value = value.replace('.', 'x');   //
// or
value = value.replace(/\./g, 'x'); // replaces all '.'

Replace all dots to single

We can phrase the replacement as being any single dot, which in turn is followed by a space and another dot, that entire quantity occurring one or more times. That is, we can find on the following pattern and then replace with just a single dot:

\.( \.)+

Code sample:

var input = ". . . Hello . . . . . . world . . .";console.log(input);input = input.replace(/\.( \.)+/g, ".");console.log(input);

javascript replace() not working with 3 dots

You need to escape the dots and make the regex global:

var updatedStr = str.replace(/\.\.\./g, '​');

How to replace dots to commas and commas to dots in js

Use the variant of the replaceAll function that takes a function as the second parameter. Use it to replace all occurrences of either dot or comma. The function will choose to replace dots with commas and commas with dots.

function switchDotsAndCommas(s) {

function switcher(match) {
// Switch dot with comma and vice versa
return (match == ',') ? '.' : ',';
}

// Replace all occurrences of either dot or comma.
// Use function switcher to decide what to replace them with.
return s.replaceAll(/\.|\,/g, switcher);
}

console.log(switchDotsAndCommas('10.499,99'));
console.log(switchDotsAndCommas('99,999,999,999.99'));

How to replace every dot in string with a / in JavaScript

You can target all the periods with a regex that uses the global modifier, just remember to escape the period as periods have special meaning in a regex (as you've experienced, they match any character) :

var myDate = "11.23.13";
var myDateWithNewSeparator = myDate.replace(/\./g, '/');

How do i replace all dot in string to Underscore?

You don't even need help here from any library, this is just plain Javascript:

var str = "4.1.1.1";alert(str.replace(/\./g, '_'));

removing dot symbol from a string

Split the string on all the .'s and then join it again with empty spaces, like this:

checkedNew = checked.split('.').join("");

Remove dots and spaces from strings

You can use

s.replace(/(\d)[\s.]+(?=\d)/g, '$1')
s.replace(/(?<=\d)[\s.]+(?=\d)/g, '')

See the regex demo.

Details

  • (\d) - Group 1 ($1 in the replacement pattern is the value of the group): a digit
  • [\s.]+ - one or more whitespace or . chars
  • (?=\d) - a positive lookahead that ensures the next char is a digit.

See JavaScript demo:

const text = 'This is an 8-string 12.34.5678; and this is another 13-string 1234 5678 9123 0 okay?';
console.log(text.replace(/(\d)[\s.]+(?=\d)/g, '$1'));


Related Topics



Leave a reply



Submit