marijnh / Eloquent-JavaScript

The sources for the Eloquent JavaScript book
https://eloquentjavascript.net
2.99k stars 789 forks source link

good to use [^] to overcome the dotall problem -- I think [.\n] is equally good #514

Closed KennethKinLum closed 4 years ago

KennethKinLum commented 4 years ago

(in p.155 of your book, on the topic Greed, of RegExp)

I think the dotall problem is related to the missing s flag such as in Firefox.

Now we can use [^] and that's nice way. I also read that [\s\S] (or any pair such as [\w\W]) can also work.

Now I think [.\n] is also good and worth a mentioning because it gives a clear indication of what wants to be matched, than any other forms out there.

marijnh commented 4 years ago

Given that /[.\n]/.test("x") returns false, I suspect this doesn't do what you think it does. /(.|\n)/ would work. But I don't think it's worthwhile to add noise to the text with different suggestions when [^] does its job just fine.

KennethKinLum commented 4 years ago

ah... i forgot the . in the [] has special meaning... oops... is there a way to make . mean the same thing even inside the []?

marijnh commented 4 years ago

I don't think there is

KennethKinLum commented 4 years ago

ok, then it can be (.|\n) but I guess it maybe a bit more verbose than [^] or [\s\S] but it is more clear than [^] or [\s\S]

but then...

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes#Types

. Matches any single character except line terminators: \n, \r, \u2028 or \u2029. For example, /.y/ matches "my" and "ay", but not "yes", in "yes make my day".

that's why "a\r\nb".match(/a(.|\n)*b/) won't match.