simplify escapeRegExp regexp

This commit is contained in:
rolux 2012-05-29 12:59:41 +02:00
parent 56432a67bc
commit c5fdc1ab39

View file

@ -4,10 +4,10 @@ Ox.escapeRegExp <f> Escapes a string for use in a regular expression
str <s> String str <s> String
> Ox.escapeRegExp('foo.com/bar?baz') > Ox.escapeRegExp('foo.com/bar?baz')
'foo\\.com\\/bar\\?baz' 'foo\\.com\\/bar\\?baz'
> new RegExp(Ox.escapeRegExp('foo.com/bar?baz')).test('foo.com/bar?baz') > new RegExp(Ox.escapeRegExp('/\\^$*+?.-|(){}[]')).test('/\\^$*+?.-|(){}[]')
true true
@*/ @*/
// see https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions // see https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
Ox.escapeRegExp = function(string) { Ox.escapeRegExp = function(string) {
return (string + '').replace(/([\/\\\^\$\*\+\?\.\-\|\(\)\{\}\[\]])/g, '\\$1'); return (string + '').replace(/([\/\\^$*+?.\-|(){}[\]])/g, '\\$1')
}; };