2

I've tried with replace(/ ./g, '.'); to eliminate the comma before the dot without succes. Any idea what is wrong? Thanks.

zsola3075457
  • 177
  • 4
  • 14

4 Answers4

4

I presume your complaint is that every character followed by a space is being replaced with a .. This is because . is a wildcard character. Literally, it means "match anything except a newline":

(The dot, the decimal point) matches any single character except the newline characters: \n \r \u2028 or \u2029. (MDN)

You need to escape it if you want to match a literal .:

replace(/ \./g, '.')
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
3

The way you are presenting the dot . means match any single character (except newline), the dot . is considered a special character in regular expressions and needs to be escaped.

I would tack on a quantifier also with matching the whitespace before the dot so it replaces all occurrences.

str = str.replace(/\s+\./g, '.');
hwnd
  • 69,796
  • 4
  • 95
  • 132
2

The . is a special character, you should escape it:

" .bla".replace(/ \./g, '.');
juan.facorro
  • 9,791
  • 2
  • 33
  • 41
1

Have you tried string = string.replace(/\s\./, '.');?

Arthur
  • 1,433
  • 1
  • 18
  • 35