diff --git a/.github/funding.yml b/.github/funding.yml new file mode 100644 index 0000000..5bc8a3d --- /dev/null +++ b/.github/funding.yml @@ -0,0 +1,4 @@ +github: sindresorhus +open_collective: sindresorhus +tidelift: npm/escape-string-regexp +custom: https://sindresorhus.com/donate diff --git a/.github/security.md b/.github/security.md new file mode 100644 index 0000000..5358dc5 --- /dev/null +++ b/.github/security.md @@ -0,0 +1,3 @@ +# Security Policy + +To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. diff --git a/.travis.yml b/.travis.yml index f3fa8cd..94ab01f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ language: node_js node_js: + - '12' - '10' - - '8' diff --git a/index.js b/index.js index 58217a4..e5bb9db 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,13 @@ 'use strict'; -const matchOperatorsRegex = /[|\\{}()[\]^$+*?.-]/g; - module.exports = string => { if (typeof string !== 'string') { throw new TypeError('Expected a string'); } - return string.replace(matchOperatorsRegex, '\\$&'); + // Escape characters with special meaning either inside or outside character sets. + // Use a simple backslash escape when it’s always valid, and a \unnnn escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar. + return string + .replace(/[|\\{}()[\]^$+*?.]/g, '\\$&') + .replace(/-/g, '\\u002d'); }; diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index 2e343cf..5f313f2 100644 --- a/package.json +++ b/package.json @@ -1,20 +1,17 @@ { "name": "escape-string-regexp", - "version": "2.0.0", + "version": "3.0.0", "description": "Escape RegExp special characters", "license": "MIT", "repository": "sindresorhus/escape-string-regexp", + "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, - "maintainers": [ - "Sindre Sorhus (sindresorhus.com)", - "Joshua Boy Nicolai Appelman (jbna.nl)" - ], "engines": { - "node": ">=8" + "node": ">=10" }, "scripts": { "test": "xo && ava && tsd" @@ -27,17 +24,15 @@ "escape", "regex", "regexp", - "re", "regular", "expression", "string", - "str", "special", "characters" ], "devDependencies": { "ava": "^1.4.1", - "tsd": "^0.7.2", - "xo": "^0.24.0" + "tsd": "^0.11.0", + "xo": "^0.28.3" } } diff --git a/readme.md b/readme.md index 157472b..2945dfc 100644 --- a/readme.md +++ b/readme.md @@ -2,14 +2,12 @@ > Escape RegExp special characters - ## Install ``` $ npm install escape-string-regexp ``` - ## Usage ```js @@ -23,7 +21,14 @@ new RegExp(escapedString); You can also use this to escape a string that is inserted into the middle of a regex, for example, into a character class. - -## License - -MIT © [Sindre Sorhus](https://sindresorhus.com) +--- + +
+ + Get professional support for this package with a Tidelift subscription + +
+ + Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies. +
+
diff --git a/test.js b/test.js index 7fac094..fe5f549 100644 --- a/test.js +++ b/test.js @@ -11,6 +11,13 @@ test('main', t => { test('escapes `-`', t => { t.is( escapeStringRegexp('foo - bar'), - 'foo \\- bar' + 'foo \\u002d bar' + ); +}); + +test('escapes `-` in a way compatible with the Unicode flag', t => { + t.regex( + '-', + new RegExp(escapeStringRegexp('-'), 'u') ); });