tc39 / proposal-regex-escaping

Proposal for investigating RegExp escaping for the ECMAScript standard
http://tc39.es/proposal-regex-escaping/
Creative Commons Zero v1.0 Universal
363 stars 32 forks source link

Incorrect example usage #63

Closed InSyncWithFoo closed 11 months ago

InSyncWithFoo commented 11 months ago

I'm talking about this one:

RegExp.escape("\d \D (?:)"); // "\\d \\D \(\?\:\)"

The string "\d \D (?:)" has 8 characters and no backslashes. Isn't this supposed to be either "\\d \\D (?:)" or String.raw`\d \D (?:)`?

Perhaps other examples can also use some help in this manner:

RegExp.escape("Buy it. use it. break it. fix it.") // "Buy it\\. use it\\. break it\\. fix it\\."
RegExp.escape("(*.*)"); // "\\(\\*\\.\\*\\)"
RegExp.escape("。^・ェ・^。") // "。\\^・ェ・\\^。"
RegExp.escape("😊 *_* +_+ ... 👍"); // "😊 \\*_\\* \\+_\\+ \\.\\.\\. 👍"
RegExp.escape("\\d \\D (?:)"); // "\\\\d \\\\D \\(\\?\\:\\)"

I agree, multiple backslashes are indeed horrible, so something like this would also suffice:

console.log(RegExp.escape("Buy it. use it. break it. fix it."))  // Buy it\. use it\. break it\. fix it\.
console.log(RegExp.escape("(*.*)"))  // \(\*\.\*\)
console.log(RegExp.escape("。^・ェ・^。"))  // 。\^・ェ・\^。
console.log(RegExp.escape("😊 *_* +_+ ... 👍"))  // 😊 \*_\* \+_\+ \.\.\. 👍
console.log(RegExp.escape(String.raw`\d \D (?:)`))  // \\d \\D \(\?\:\)

But again, maybe I'm just being too pedantic.

ljharb commented 11 months ago

Based on the updated proposal, ("\d \D (?:)" should escape to \(\"\\d\ \\D\ \(\?\:\)", if I'm following the algorithm correctly.

bakkot commented 11 months ago

@ljharb the problem is that the string "\d \D (?:)" is the same as the string "d D (?:)". i.e. "\d \D (?:)" === "d D (?:)".

If you want to have a string with backslashes in, you need to either escape the backslashes or use String.raw.

ljharb commented 11 months ago

ahhhh gotcha. meaning that the text content \d \D (?:) is the string d D (?:) which should then escape to the string d\ D\ \(\?\:\)

I'll update the readme to correct this, thanks.