marijnh / Eloquent-JavaScript

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

Ch. 9 – Regular expressions #402

Closed mikegowen closed 6 years ago

mikegowen commented 6 years ago

In this example: http://eloquentjavascript.net/09_regexp.html#c_5E2M1BBsUm

let quotedText = /'([^']*)'/;
console.log(quotedText.exec("she said 'hello'"));
// → ["'hello'", "hello"]

Would (.*) be more appropriate here since you're already covering the apostrophes at the beginning and end of the regular expression? Especially since something like "she said 'what's up'" would break the regular expression currently shown (not that it actually matters for this specific example).

marijnh commented 6 years ago

Would (.*) be more appropriate here

No — as you pointed out, that'd skip single quotes when it could, which isn't what this expression is trying to do.