It seems like the dot character in Javascript’s regular expressions matches any character except new line and no number of modifiers could change that. Sometimes you just want to match everything and there’s a couple of ways to do that.
You can pick an obscure character and apply a don’t match character range with it ie [^`]+
. This is not true match any character though. Or you can try [.\r\n]+
which doesn’t seem to work at all. (?:\r|\n|.)+
works fine, but as you’ll find out soon, it is notoriously slow as each time you use it, you are creating a new 3 way branching point because of the brackets.
The perfect way I’ve found is actually a nicer variation of the first idea:
[^]+
Which means ‘don’t match no characters’, a double negative that can re-read as ‘match any character’. Hacky, but works perfectly.
Pingback: code.gnysek.pl » Blog Archive » JavaScript: dowolny znak włącznie ze znakiem nowej linii
Pingback: Steve Hannah: This week » Javascript matching all characters including new line
Thank you! That was exactly what I was looking for :) Shame the Javascript designers removed the standard /m flag.
‘A\nB’.match(/[^]+\nB/) fails in Internet Explorer 9. Use [\S\s] instead!
Nice hack, but it’s this slower then .* for example ?
You’ve saved me an hour of headache with this! – Wish i had this level of creative and out-of-the box thinking.
Wow, thank you very much! Stackoverflow this pls.
A very, very great idea!!! :-) Many thanks!
Works perfectly!
I had to do search and replace so I modified the code as .replace(//mgi, “”)
Thanks again..
Thank you!
spent days trying to figure out matching between characters that have multi line & this man comes up with just [^]+ which gets any char in between, a lot of people always say they know their stuff but a few people really know their stuff; this man is one of them.
this removes everything except a square empty box.
[^\\u25A1]+
this removes everything up to & including the empty square box & the space after it.
[^\\u25A1]+\\u25A1\\s+
thank you so much was looking for this regex recnik all day long