Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript multiline regexp replace

"foo\r\nbar".replace(/(foo).+/m, "bar") 

Hello. I can not understand why this code does not replace foo on bar

like image 590
Andrey Kuznetsov Avatar asked Apr 04 '10 20:04

Andrey Kuznetsov


1 Answers

I can not understand why this code does not replace foo on bar

Because the dot . explicitly does not match newline characters.

This would work:

"foo\r\nbar".replace(/foo[\s\S]+/m, "bar") 

because newline characters count as whitespace (\s).

Note that the parentheses around foo are superfluous, grouping has no benefits here.

like image 59
Tomalak Avatar answered Oct 14 '22 08:10

Tomalak