Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript string regex /.*/gm does not catch all line at once

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?

like image 814
Julio Saito Avatar asked Mar 01 '12 14:03

Julio Saito


People also ask

What does GM do in regex?

Definition and Usage The "g" modifier specifies a global match. A global match finds all matches (compared to only the first).

How do I return all matches in regex?

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.

What is /\ S G in regex?

/\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.

How do you search for a regex pattern at the beginning of a string?

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.


1 Answers

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 = ''
like image 88
T.J. Crowder Avatar answered Oct 02 '22 11:10

T.J. Crowder