I saw String(value).replace(/[^0-9.-]/g, '') from a library that described to filter illegal characters, and finally it will return the legal number. Why does this work and what will really be replaced? I feel confused because I think nothing will be replaced.
For example:
String("1.absd").replace(/[^0-9.-]/g, '')
// '1.'
String(value) will make string from value. So if you use number, this will convert it to string, because function replace is defined only from string.replace(/[^0-9.-]/g, '') this will replace any matching given regular expression with empty string. In regular expression you have [^0-9.-]. The ^ at start means, that any characters not from this interval will match. So any character other than number, dot or -. Modifier g after slash means, that this regular expression is global and will be applied for all matchesso from "1.absd" the characters "absd" will be replaced with empty string