escapeStringForRegex.js 2.1 KB

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