oxjs/source/Ox/js/RegExp.js

13 lines
478 B
JavaScript
Raw Permalink Normal View History

2012-05-19 06:36:02 +00:00
/*@
Ox.escapeRegExp <f> Escapes a string for use in a regular expression
(str) -> <r> Escaped string
str <s> String
2012-05-27 16:41:14 +00:00
> Ox.escapeRegExp('foo.com/bar?baz')
'foo\\.com\\/bar\\?baz'
2012-05-29 10:59:41 +00:00
> new RegExp(Ox.escapeRegExp('/\\^$*+?.-|(){}[]')).test('/\\^$*+?.-|(){}[]')
2012-05-27 16:41:14 +00:00
true
2012-05-19 06:36:02 +00:00
@*/
// see https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
2012-05-25 12:32:17 +00:00
Ox.escapeRegExp = function(string) {
2014-08-19 08:19:59 +00:00
return (string + '').replace(/([\/\\^$*+?.\-|(){}[\]])/g, '\\$1');
2012-05-19 06:36:02 +00:00
};