escapeStringForRegex.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. // Based on https://github.com/sindresorhus/escape-string-regexp but with modifications to:
  3. // a) reduce the size by skipping the runtime type - checking
  4. // b) ensure it gets down - compiled for old versions of Node(the published package only supports Node 12+).
  5. //
  6. // MIT License
  7. //
  8. // Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  11. // documentation files(the "Software"), to deal in the Software without restriction, including without limitation
  12. // the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and
  13. // to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of
  16. // the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  19. // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. // IN THE SOFTWARE.
  23. /**
  24. * Given a string, escape characters which have meaning in the regex grammar, such that the result is safe to feed to
  25. * `new RegExp()`.
  26. *
  27. * @param regexString The string to escape
  28. * @returns An version of the string with all special regex characters escaped
  29. */
  30. function escapeStringForRegex(regexString) {
  31. // escape the hyphen separately so we can also replace it with a unicode literal hyphen, to avoid the problems
  32. // discussed in https://github.com/sindresorhus/escape-string-regexp/issues/20.
  33. return regexString.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
  34. }
  35. exports.escapeStringForRegex = escapeStringForRegex;
  36. //# sourceMappingURL=escapeStringForRegex.js.map