How to use the escapeRegExp function from lodash

Find comprehensive JavaScript lodash.escapeRegExp code examples handpicked from public code repositorys.

lodash.escapeRegExp is a function in the Lodash library that escapes any special characters in a string that would have a special meaning in a regular expression.

102
103
104
105
106
107
108
109
110
111
module.exports.entries             = _.entries;
module.exports.entriesIn           = _.entriesIn;
module.exports.eq                  = _.eq;
module.exports.eqContrib           = _.eqContrib;
module.exports.escape              = _.escape;
module.exports.escapeRegExp        = _.escapeRegExp;
module.exports.every               = _.every;
module.exports.exists              = _.exists;
module.exports.existsAll           = _.existsAll;
module.exports.explode             = _.explode;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

799
800
801
802
803
804
805
806
807
808
809
810
811
console.log(endsWith); // => true


const escape = _.escape('fred, barney, & pebbles');
console.log(escape); // => 'fred, barney, & pebbles'


const escapeRegExp = _.escapeRegExp('[lodash](https://lodash.com/)');
console.log(escapeRegExp); // => '\[lodash\]\(https://lodash\.com/\)'


const kebabCase = _.kebabCase('Foo Bar');
console.log(kebabCase); // => 'foo-bar'
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.escapeRegExp work?

lodash.escapeRegExp is a function provided by the Lodash library which takes a string as input and escapes any characters in the string that would have special meaning in a regular expression, such as backslashes and parentheses, by adding a backslash before them.

66
67
68
69
70
71
72
73
74
75
76
console.log(_.escape('<h1>Heading</h1>'));
console.log(_.unescape('<h1>Heading</h1>'));
console.log(_.unescape('&lt;h1&gt;Heading&lt;/h1&gt;'));
console.log(_.escapeRegExp('https://www.lodash.com'));
console.log(
  _.escapeRegExp('https://www.lodash.com?param={myvalue}|param2={myvalue2}')
);


console.log(_.parseInt('8'));
console.log(_.parseInt('08'));
fork icon0
star icon0
watch icon0

118
119
120
121
122
123
124
125
126
127
const projectList = await ctx.model.Project._find();
const tagList = await ctx.model.Tag._find();

if (key) {
  queryCond.$or = [];
  queryCond.$or.push({ desc: { $regex: _.escapeRegExp(key) } });
  const matchTags = (tagList || []).filter(tag => tag.name.includes(key));
  const matchTagIds = matchTags.map(tag => tag.id);
  if (!_.isEmpty(matchTagIds)) queryCond.$or.push({ tags: { $in: matchTagIds } });
}
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
const _ = require("lodash");

const string = "Hello. How are you?";
const escapedString = _.escapeRegExp(string);

console.log(escapedString);
// Output: Hello\. How are you\?

In this example, lodash.escapeRegExp is used to escape any special characters in the string variable, which can then be used as a regular expression pattern. The resulting escaped string is stored in the escapedString variable, which is logged to the console.

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)