esnext.regexp.escape.js 774 B

1234567891011121314151617181920
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var toString = require('../internals/to-string');
  5. var WHITESPACES = require('../internals/whitespaces');
  6. var charCodeAt = uncurryThis(''.charCodeAt);
  7. var replace = uncurryThis(''.replace);
  8. var NEED_ESCAPING = RegExp('[!"#$%&\'()*+,\\-./:;<=>?@[\\\\\\]^`{|}~' + WHITESPACES + ']', 'g');
  9. // `RegExp.escape` method
  10. // https://github.com/tc39/proposal-regex-escaping
  11. $({ target: 'RegExp', stat: true, forced: true }, {
  12. escape: function escape(S) {
  13. var str = toString(S);
  14. var firstCode = charCodeAt(str, 0);
  15. // escape first DecimalDigit
  16. return (firstCode > 47 && firstCode < 58 ? '\\x3' : '') + replace(str, NEED_ESCAPING, '\\$&');
  17. }
  18. });