Two equal lines representing spaces are not equal in JS

I had a problem trying to replace spaces in a number. For example, this works, that is, returns 27721, as expected:

alert("27 721".replace(/ /g, ""));
Run codeHide result

While - I don’t know why - this is not so (my browser is in French, so a thousand separators is a space):

function getThousandSeparator() {
  var testN = 1000;
  return testN.toLocaleString().replace(/\d/g,"");
}
alert("27 721".replace(new RegExp(getThousandSeparator(), "g"), ""));
Run codeHide result

And if I force the function to directly return "", then it works. Also, if you test:

console.log(getThousandSeparator() == " ");

it shows false ... Thank you in advance.

+4
source share
1 answer

In my testing, the delimiter character is actually  (non-breaking space), not real space.

+1

Source: https://habr.com/ru/post/1608484/


All Articles