Last active
April 2, 2022 10:11
-
-
Save think49/7691225 to your computer and use it in GitHub Desktop.
escaperegexpchar.js: 正規表現文字列をエスケープします(ES5準拠)。エスケープ文字列は new RegExp() に渡すことができます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* escaperegexpchar.js | |
* Escape the regular expression string (ES5 compliant). | |
* Escape string can be passed to the () new RegExp(). | |
* | |
* @version 1.0.2 | |
* @author think49 | |
* @url https://gist.github.com/think49/7691225 | |
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License) | |
* @see <a href="http://es5.github.io/#x15.10.1">15.10.1 Patterns - Annotated ES5.1</a> | |
*/ | |
'use strict'; | |
function escapeRegExpChar (string) { | |
return String(string).replace(/(?=[$()*+\-.?\[\]^{|}])/g, '\\'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment