escapeRegExp
string
Escapes the RegExp special characters "^", "$", "\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in string.
Installation
Import
import { escapeRegExp } from '@tulx/utils';Source Code
Implementation
/**
* Escapes the RegExp special characters "^", "$", "\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in string.
*
* @param string - The string to escape.
* @returns Returns the escaped string.
*
* @example
* ```ts
* escapeRegExp('[lodash](https://lodash.com/)'); // '\\[lodash\\]\\(https://lodash\\.com/\\)'
* ```
*/
export function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
Example
import { escapeRegExp } from '@tulx/utils';
escapeRegExp('[lodash](https://lodash.com/)'); // '\\[lodash\\]\\(https://lodash\\.com/\\)'Related Functions
camelCase
Converts string to camel case.
capitalize
Converts the first character of string to upper case and the remaining to lower case.
deburr
Deburrs string by converting Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and removing combining diacritical marks.
endsWith
Checks if string ends with the given target string.
escape
Converts the characters "&", "<", ">", '"', and "'" in string to their corresponding HTML entities.
kebabCase
Converts string to kebab case.