escapeRegex

escapeRegex

Escapes regex chars

If you need to use any of the special characters literally (actually searching for a "", for instance), you must escape it by putting a backslash in front of it. For instance, to search for "a" followed by "" followed by "b", you'd use /a*b/ — the backslash "escapes" the "*", making it literal instead of special. Read more on MDN (opens in a new tab)

Import

import { escapeRegex } from '@fullstacksjs/toolbox';

Signature

function escapeRegex(s: string): string {}

Examples

new RegExp(escapeRegex('^[](){}$')).test('^[](){}$') // true
new RegExp(escapeRegex('^a$')).test('a')             // false