Regular Expression Utilities API reference for regular expression utility functions asterisk Guide

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

NameTypeDescription
stringstringThe 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\)\?"

escapeHTML

function escapeHTML(string)

Escapes HTML special characters in a string.

Parameters

NameTypeDescription
stringstringThe string to escape

Returns

A new string with HTML special characters escaped.

Example

import { escapeHTML } from '@semantic-ui/utils';
const str = '<script>alert("XSS")</script>';
console.log(escapeHTML(str)); // "&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt;"