On This Page
Functions
Regular Expression Utilities
The Regular Expression utilities provide functions for working with regular expressions and string patterns in JavaScript. These functions help in creating, escaping, and applying regular expressions efficiently.
Functions
escapeRegExp
function escapeRegExp(string)Escapes special characters in a string for use in a regular expression.
This function escapes all special regex characters
[ ] { } ( ) / \ ^ $ . | ? * +which is helpful when inserting a dynamic string into a regex like a user search query.
Parameters
| Name | Type | Description |
|---|---|---|
| string | string | The string to escape |
Returns
A new string with all regular expression special characters escaped.
Example
import { escapeRegExp } from '@semantic-ui/utils';
const str = "How much for the (cat)?";console.log(escapeRegExp(str)); // "How much for the \(cat\)\?"