Skip to content

Commit

Permalink
add RegExp.escape
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 1, 2023
1 parent a080c51 commit ea68a15
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,8 @@ export const data = {
// TODO: Remove from `core-js@4`
'esnext.reflect.metadata': {
},
'esnext.regexp.escape': {
},
'esnext.set.add-all': {
},
'esnext.set.delete-all': {
Expand Down
3 changes: 3 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,7 @@ export default {
'esnext.data-view.set-uint8-clamped',
'esnext.math.f16round',
],
3.33: [
'esnext.regexp.escape',
],
};
5 changes: 5 additions & 0 deletions packages/core-js/full/regexp/escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';
require('../../modules/esnext.regexp.escape');
var path = require('../../internals/path');

module.exports = path.RegExp.escape;
1 change: 1 addition & 0 deletions packages/core-js/full/regexp/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
var parent = require('../../actual/regexp');
require('../../modules/esnext.regexp.escape');

module.exports = parent;
20 changes: 20 additions & 0 deletions packages/core-js/modules/esnext.regexp.escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var toString = require('../internals/to-string');
var WHITESPACES = require('../internals/whitespaces');

var charCodeAt = uncurryThis(''.charCodeAt);
var replace = uncurryThis(''.replace);
var NEED_ESCAPING = RegExp('[!"#$%&\'()*+,\\-./:;<=>?@[\\\\\\]^`{|}~' + WHITESPACES + ']', 'g');

// `RegExp.escape` method
// https://github.com/tc39/proposal-regex-escaping
$({ target: 'RegExp', stat: true, forced: true }, {
escape: function escape(S) {
var str = toString(S);
var firstCode = charCodeAt(str, 0);
// escape first DecimalDigit
return (firstCode > 47 && firstCode < 58 ? '\\x3' : '') + replace(str, NEED_ESCAPING, '\\$&');
}
});
3 changes: 3 additions & 0 deletions packages/core-js/proposals/regexp-escaping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
// https://github.com/tc39/proposal-regex-escaping
require('../modules/esnext.regexp.escape');
1 change: 1 addition & 0 deletions packages/core-js/stage/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require('../proposals/array-is-template-object');
require('../proposals/async-iterator-helpers');
require('../proposals/iterator-range');
require('../proposals/map-upsert-stage-2');
require('../proposals/regexp-escaping');
require('../proposals/string-dedent');
require('../proposals/symbol-predicates-v2');
// TODO: Obsolete versions, remove from `core-js@4`
Expand Down

0 comments on commit ea68a15

Please sign in to comment.