RegExp.escape()

Steven Levithan steves_list at hotmail.com
Fri Mar 23 12:37:35 PDT 2012


Erik Corry wrote:
> In perl the recommended version is
>
> text.replace(/([^a-zA-Z0-9])/g, "\\$1")

For the record, most languages with modern regular expressions include a 
built in method for this.

For instance:

* Perl: quotemeta(str)
* PHP: preg_quote(str)
* Python: re.escape(str)
* Ruby: Regexp.escape(str)
* Java: Pattern.quote(str)
* C#, VB.NET: Regex.Escape(str)

-- Steven Levithan



More information about the es-discuss mailing list