Skip to content

Instantly share code, notes, and snippets.

@think49
Last active April 2, 2022 10:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think49/7691225 to your computer and use it in GitHub Desktop.
Save think49/7691225 to your computer and use it in GitHub Desktop.
escaperegexpchar.js: 正規表現文字列をエスケープします(ES5準拠)。エスケープ文字列は new RegExp() に渡すことができます。
/**
* 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