You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 18, 2025. It is now read-only.
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.
10
12
11
13
12
14
## Motivation
@@ -16,14 +18,17 @@ It is often the case when we want to build a regular expression out of a string
16
18
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.
17
19
18
20
19
-
## Proposed solution and usage examples
21
+
## Possible solutions:
20
22
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:
22
26
23
27
```js
24
-
var str =prompt("Please enter a string");
25
-
str =RegExp.escape(str);
26
-
alert(ourLongText.replace(newRegExp(str, "g")); // handles reg exp special tokens with the replacement.
28
+
conststr=prompt("Please enter a string");
29
+
constescaped=RegExp.escape(str);
30
+
constre=newRegExp(escaped, 'g'); // handles reg exp special tokens with the replacement.
This would be, for example, a template tag function `RegExp.tag`, used to produce a regular expression:
46
+
47
+
```js
48
+
conststr=prompt("Please enter a string");
49
+
constre=RegExp.tag`/${str}/g`;
50
+
console.log(ourLongText.replace(re));
51
+
```
52
+
38
53
## Cross-cutting concerns
39
54
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.
41
56
42
57
43
58
## In other languages
@@ -67,7 +82,7 @@ We've had [a meeting about this subject](https://github.com/benjamingr/RegExp.es
67
82
***What about the `/` character?**
68
83
69
84
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
+
71
86
***What about the `,` character?**
72
87
73
88
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
84
99
85
100
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.
86
101
102
+
<!--
87
103
* **Why not `RegExp.tag` or another tagged template based proposal?**
88
104
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:
90
106
- 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.
91
107
- 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.
92
108
- 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).
93
109
- This proposal does not block effort on a tag proposal, the two proposals are not mutually exclusive and both APIs can eventually land.
94
110
See [this issue](https://github.com/benjamingr/RegExp.escape/issues/45) for discussion.
95
-
111
+
112
+
-->
96
113
***Why don't you do X?**
97
114
98
115
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