views:

77

answers:

2

Would the second match a line ending, while the first would not?

+4  A: 

Yes, AFAIK the dot does not match newlines in most regex engines without a modifier.

EDIT: Apparently JS doesn't even have that option. I personally think negated character classes are the way to go; I barely use the dot in regex.

quantumSoup
`[\s\S]` is the most common idiom for "anything including a newline" in JavaScript. And +1 for avoiding the dot; it's a good way to improve your regex-fu, by forcing yourself to think about what the regex engine is doing.
Alan Moore
+2  A: 

Another important difference is that the dot will match ). Suppose you're you're trying to match the first parenthetical expression in

blah blah (foo) blah blah (bar)

The regex /\(.*\)/ will match (foo) blah blah (bar) because the * is greedy. You can fix that by using a reluctant quantifier instead - /\(.*?\)/ - but what if you want to match the last one? You know it's the last thing in the string, so you just add the end-of-string anchor - /\(.*?\)$/ - but now it's back to matching (foo) blah blah (bar) again. Only the negated character class will give you what you want in this case: /\([^)]*\)$/.

Alan Moore