Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit b2d67df

Browse files
committed
[readme] update to reframe as problem investigation
1 parent 3c3590f commit b2d67df

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

Diff for: README.md

+30-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# `RegExp.escape` Proposal
1+
# RegExp Escaping Proposal
22

3-
Proposal for adding a `RegExp.escape` method to the ECMAScript standard.
3+
This ECMAScript proposal seeks to investigate the problem area of escaping a string for use inside a Regular Expression.
44

5-
[Formal specification](http://benjamingr.github.io/RegExp.escape/)
5+
<!--
6+
[Formal specification](http://tc39-transfer.github.io/proposal-regex-escaping)
7+
-->
68

79
## Status
810

9-
This proposal is a [stage 0 (strawman) proposal](https://docs.google.com/document/d/1QbEE0BsO4lvl7NFTn5WXWeiEIBfaVUF7Dk0hpPpPDzU/edit#) and is awaiting implementation and more input. Please see [the issues](https://github.com/benjamingr/RegExp.escape/issues) on how to get involved.
11+
This proposal is a [stage 1 proposal](https://github.com/tc39/proposals/blob/master/stage-1-proposals.md) and is awaiting implementation and more input. Please see [the issues](https://github.com/tc39-transfer/proposal-regex-escaping/issues) to get involved.
1012

1113

1214
## Motivation
@@ -16,14 +18,17 @@ It is often the case when we want to build a regular expression out of a string
1618
This is commonly-desired functionality, as can be seen from [this years-old es-discuss thread](https://esdiscuss.org/topic/regexp-escape). Standardizing it would be very useful to developers, and avoid subpar implementations they might create that could miss edge cases.
1719

1820

19-
## Proposed solution and usage examples
21+
## Possible solutions:
2022

21-
We propose the addition of an `RegExp.escape` function, such that strings can be escaped in order to be used inside regular expressions:
23+
### `RegExp.escape` function
24+
25+
This would be a `RegExp.escape` function, such that strings can be escaped in order to be used inside regular expressions:
2226

2327
```js
24-
var str = prompt("Please enter a string");
25-
str = RegExp.escape(str);
26-
alert(ourLongText.replace(new RegExp(str, "g")); // handles reg exp special tokens with the replacement.
28+
const str = prompt("Please enter a string");
29+
const escaped = RegExp.escape(str);
30+
const re = new RegExp(escaped, 'g'); // handles reg exp special tokens with the replacement.
31+
console.log(ourLongText.replace(re));
2732
```
2833

2934
```js
@@ -35,9 +40,19 @@ RegExp.escape("😊 *_* +_+ ... 👍"); // "😊 \*_\* \+_\+ \.\.\. 👍"
3540
RegExp.escape("\d \D (?:)"); // "\\d \\D \(\?\:\)"
3641
```
3742

43+
### Template tag function
44+
45+
This would be, for example, a template tag function `RegExp.tag`, used to produce a regular expression:
46+
47+
```js
48+
const str = prompt("Please enter a string");
49+
const re = RegExp.tag`/${str}/g`;
50+
console.log(ourLongText.replace(re));
51+
```
52+
3853
## Cross-cutting concerns
3954

40-
The list of escaped identifiers should be kept in sync with what the regular expression grammar considers to be syntax characters that need escaping. For this reason, instead of hard-coding the list of escaped characters, we escape characters that are recognized as `SyntaxCharacter`s by the engine. For example, if regexp comments are ever added to the specification (presumably under a flag), this ensures that they are properly escaped.
55+
The list of escaped identifiers should be kept in sync with what the regular expression grammar considers to be syntax characters that need escaping. For this reason, instead of hard-coding the list of escaped characters, we escape characters that are recognized as `SyntaxCharacter`s by the engine. For example, if regexp comments are ever added to the specification (presumably under a flag), this ensures that they are properly escaped. Additionally, named capture groups must be accounted for.
4156

4257

4358
## In other languages
@@ -67,7 +82,7 @@ We've had [a meeting about this subject](https://github.com/benjamingr/RegExp.es
6782
* **What about the `/` character?**
6883

6984
Empirical data has been collected (see the /data folder) from about a hundred thousand code bases (most popular sites, most popular packages, most depended on packages and Q&A sites) and it was found out that its use case (for `eval`) was not common enough to justify addition.
70-
85+
7186
* **What about the `,` character?**
7287

7388
The one obscure case where this could suggest a cause for escaping, avoiding a range for user-supplied numbers in `new RegExp('a{'+ RegExp.escape('3,5') + '}')`, does not lead to any clearly safer results with escaping, as doing so will cause the sequence `{3\,5}` to be treated as a literal (rather than say throwing with bad input that an application could recover from).
@@ -84,15 +99,17 @@ The one obscure case where this could suggest a cause for escaping, avoiding a r
8499

85100
EscapeRegExpPattern (as the name implies) takes a pattern and escapes it so that it can be represented as a string. What `RegExp.escape` does is take a string and escapes it so it can be literally represented as a pattern. The two do not need to share an escaped set and we can't use one for the other. We're discussing renaming EscapeRegExpPattern in the spec in the future to avoid confusion for readers.
86101

102+
<!--
87103
* **Why not `RegExp.tag` or another tagged template based proposal?**
88104
89-
During the last time this proposal was presented - an edge case was brought up where tagged templates were suggested as an alternative. We believe a simple function is a much better and simpler alternative to tagged templates here:
105+
During the first time this proposal was presented - an edge case was brought up where tagged templates were suggested as an alternative. We believe a simple function is a much better and simpler alternative to tagged templates here:
90106
- Users have consistently been asking for `RegExp.escape` over the past 5 years - both in this repo and elsewhere. Packages providing this functionality are very popular (see [escape-string-regexp](https://www.npmjs.com/package/escape-string-regexp) and [escape-regexp](https://www.npmjs.com/package/escape-regexp)). For comparison there are no downloads and [zero issues or interest](https://github.com/benjamingr/RegExp.tag) when I initiated work on a tag proposal.
91107
- When interviewing users regarding `RegExp.tag` when trying to get motivating use cases for the API - users spoken with were very confused because of the tagged templates. The feedback was negative enough and they found the API confusing and awkward enough for me to stop pursuing it.
92108
- Virtually every other programming language offers `.escape` (see "in other languages") and made the trade-off to ship `.escape` eventhough most of these could have shipped a tagged template API (equivalent, per language).
93109
- This proposal does not block effort on a tag proposal, the two proposals are not mutually exclusive and both APIs can eventually land.
94110
See [this issue](https://github.com/benjamingr/RegExp.escape/issues/45) for discussion.
95-
111+
112+
-->
96113
* **Why don't you do X?**
97114

98115
If you believe there is a concern that was not addressed yet, please [open an issue](https://github.com/benjamingr/RexExp.escape/issues).

0 commit comments

Comments
 (0)