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

- 177
- 4
- 14
4 Answers
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, '.')

- 233,373
- 50
- 316
- 318
-
And how is it if theese comas and dots are repeated, like . . . . Thanks – zsola3075457 Dec 07 '13 at 12:48
-
1@zsola3075457 `". . . .".replace(/ \./g, '.')` gives `....`. Is this not what you want? – lonesomeday Dec 07 '13 at 12:49
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, '.');

- 69,796
- 4
- 95
- 132
The .
is a special character, you should escape it:
" .bla".replace(/ \./g, '.');

- 9,791
- 2
- 33
- 41
Have you tried string = string.replace(/\s\./, '.');
?

- 1,433
- 1
- 18
- 35
-
Yes, this solution it is better if I have more than 1 spaces before dot, by changing the \s to \s+. Thanks – zsola3075457 Dec 07 '13 at 12:50