Why does javascript replace string function do this?
"aaa\nbbb\nccc".replace(/.*/gm, ".")
// result = "..\n..\n.." but expected was: ".\n.\n."
"aaa\nbbb\nccc".replace(/^.*/gm, ".")
// result = ".\n.\n." -> OK!!!
"aaa\nbbb\nccc".replace(/.*$/gm, ".")
// result = "..\n..\n.." but expected was: ".\n.\n."
What I am doing wrong?
Definition and Usage The "g" modifier specifies a global match. A global match finds all matches (compared to only the first).
The method str. match(regexp) finds matches for regexp in the string str . If the regexp has flag g , then it returns an array of all matches as strings, without capturing groups and other details. If there are no matches, no matter if there's flag g or not, null is returned.
/\s/g. / is the Regular Expression delimiter. It marks the beginning and end of a pattern. \s matches all space characters: '\t' , '\n' , '\r' , '\f' , ' ' , and a number of others. g means that the regex should match the string globally, so that str.
The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.
Let me address those in reverse order:
What I am doing wrong?
You want to use +
, not *
. *
means zero or more matches, which makes no sense here. +
means one or more matches. So:
"aaa\nbbb\nccc".replace(/.+/g, ".")
// ".\n.\n."
Also note that if you're not using ^
or $
(your first example), you don't need the m
modifier (but that wasn't the problem with what you were doing). And you don't need ^
or $
because .
doesn't match newlines (something I didn't know prior to answering this question).
Why does javascript replace string function do this?
I have no Earthly idea and hope someone else does.
Again, by using *
you're saying zero or more matches. So it matches all of the relevant characters, replacing them with the first dot; then it matches zero characters, replacing them with one dot. Result: Two dots.
Proof:
Live copy | Live source
"aaa\nbbb\nccc".replace(/.*/g, function(m) {
console.log("m = '" + m + "'");
});
Outputs:
m = 'aaa' m = '' m = 'bbb' m = '' m = 'ccc' m = ''
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With