oxjs/source/Ox/js/RegExp.js
2026-02-18 17:36:01 +01:00

15 lines
513 B
JavaScript

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