Last active
May 12, 2016 16:54
-
-
Save Mossuru777/e10429cb8e36f3f6fdcfbbce0e0e475d to your computer and use it in GitHub Desktop.
PHP's preg_quote in JavaScript (http://locutus.io/php/pcre/preg_quote/)
This file contains 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
module.exports = function preg_quote (str, delimiter) { // eslint-disable-line camelcase | |
// discuss at: http://locutus.io/php/preg_quote/ | |
// original by: booeyOH | |
// improved by: Ates Goral (http://magnetiq.com) | |
// improved by: Kevin van Zonneveld (http://kvz.io) | |
// improved by: Brett Zamir (http://brett-zamir.me) | |
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman) | |
// example 1: preg_quote("$40") | |
// returns 1: '\\$40' | |
// example 2: preg_quote("*RRRING* Hello?") | |
// returns 2: '\\*RRRING\\* Hello\\?' | |
// example 3: preg_quote("\\.+*?[^]$(){}=!<>|:") | |
// returns 3: '\\\\\\.\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:' | |
return (str + '') | |
.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment