oxjs/source/Ox/js/RegExp.js
2026-02-09 18:59:12 +01:00

15 lines
496 B
JavaScript

'use strict';
/*@
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
export function escapeRegExp(string) {
return (string + '').replace(/([\/\\^$*+?.\-|(){}[\]])/g, '\\$1');
};