What is @stdlib/utils-escape-regexp-string?
@stdlib/utils-escape-regexp-string is a utility package that provides functionality to escape special characters in a string so that it can be used within a regular expression. This is particularly useful when you need to dynamically create regular expressions from user input or other sources that may contain characters with special meanings in regex.
What are @stdlib/utils-escape-regexp-string's main functionalities?
Escape special characters in a string
This feature allows you to escape all special characters in a string so that the string can be safely used in a regular expression. In the example, the input string contains characters like '.', '?', and '()' which are escaped to '\.', '\?', and '\(\)' respectively.
const escapeRegExpString = require('@stdlib/utils-escape-regexp-string');
const str = 'Hello. How are you? (I hope you are well)';
const escapedStr = escapeRegExpString(str);
console.log(escapedStr); // Output: 'Hello\. How are you\? \(I hope you are well\)'
Other packages similar to @stdlib/utils-escape-regexp-string
escape-string-regexp
The 'escape-string-regexp' package provides similar functionality to @stdlib/utils-escape-regexp-string. It escapes special characters in a string to make it safe for use in a regular expression. The main difference is that 'escape-string-regexp' is a smaller, more focused package with a single purpose, whereas @stdlib/utils-escape-regexp-string is part of the larger @stdlib library which includes a wide range of utilities.
lodash.escaperegexp
The 'lodash.escaperegexp' function is part of the Lodash library, which is a utility library offering a wide range of functions for common programming tasks. 'lodash.escaperegexp' escapes special characters in a string for use in a regular expression. Compared to @stdlib/utils-escape-regexp-string, Lodash is a more comprehensive library with many additional utilities beyond just escaping regex strings.
rescape
Escape a regular expression string or pattern.
Installation
npm install @stdlib/utils-escape-regexp-string
Usage
var rescape = require( '@stdlib/utils-escape-regexp-string' );
rescape( str )
Escapes a regular expression string
or pattern.
var str = rescape( '/[A-Z]*/' );
str = rescape( '[A-Z]*' );
If provided a value which is not a primitive string
, the function throws a TypeError
.
try {
rescape( null );
} catch ( err ) {
console.error( err );
}
Examples
var rescape = require( '@stdlib/utils-escape-regexp-string' );
var out = rescape( '/beep/' );
out = rescape( 'beep' );
out = rescape( '/[A-Z]*/' );
out = rescape( '[A-Z]*' );
out = rescape( '/\\\//ig' );
out = rescape( '\\\/' );
out = rescape( '/[A-Z]{0,}/' );
out = rescape( '[A-Z]{0,}' );
out = rescape( '/^boop$/' );
out = rescape( '^boop$' );
out = rescape( '/(?:.*)/' );
out = rescape( '(?:.*)' );
out = rescape( '/(?:beep|boop)/' );
out = rescape( '(?:beep|boop)' );
Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
License
See LICENSE.
Copyright
Copyright © 2016-2021. The Stdlib Authors.