or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md
tile.json

index.mddocs/

0

# lodash.escaperegexp

1

2

Escapes RegExp special characters in strings, enabling safe use of user input or dynamic content within regular expressions. This modular export from the lodash library allows you to include only the escapeRegExp functionality without the entire lodash dependency.

3

4

## Package Information

5

6

- **Package Name**: lodash.escaperegexp

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install lodash.escaperegexp`

10

11

## Core Imports

12

13

```javascript

14

const escapeRegExp = require('lodash.escaperegexp');

15

```

16

17

## Basic Usage

18

19

```javascript

20

const escapeRegExp = require('lodash.escaperegexp');

21

22

// Escape special characters for safe RegExp construction

23

const userInput = '[lodash](https://lodash.com/)';

24

const escaped = escapeRegExp(userInput);

25

console.log(escaped);

26

// => '\\[lodash\\]\\(https://lodash\\.com/\\)'

27

28

// Use escaped string in RegExp

29

const regex = new RegExp(escaped);

30

const text = 'Check out [lodash](https://lodash.com/) for utilities!';

31

console.log(regex.test(text)); // => true

32

33

// Safe search and replace

34

const searchTerm = '*.js';

35

const escapedTerm = escapeRegExp(searchTerm);

36

const pattern = new RegExp(escapedTerm, 'g');

37

const filename = 'my-script*.js matches *.js pattern';

38

console.log(filename.replace(pattern, 'REDACTED'));

39

// => 'my-script REDACTED matches REDACTED pattern'

40

```

41

42

## Capabilities

43

44

### String Escaping

45

46

Escapes all RegExp special characters in a string for safe dynamic regular expression construction.

47

48

```javascript { .api }

49

/**

50

* Escapes the RegExp special characters "^", "$", "\\", ".", "*", "+",

51

* "?", "(", ")", "[", "]", "{", "}", and "|" in string.

52

*

53

* @param {string} [string=''] The string to escape.

54

* @returns {string} Returns the escaped string.

55

*/

56

function escapeRegExp(string)

57

```

58

59

**Parameters:**

60

- `string` (string, optional): The string to escape. Defaults to empty string if not provided.

61

62

**Returns:**

63

- (string): The escaped string with all RegExp special characters prefixed with backslashes.

64

65

**Special Character Handling:**

66

The function escapes these RegExp special characters:

67

- `^``\\^` (caret)

68

- `$``\\$` (dollar)

69

- `\\``\\\\` (backslash)

70

- `.``\\.` (dot)

71

- `*``\\*` (asterisk)

72

- `+``\\+` (plus)

73

- `?``\\?` (question mark)

74

- `(``\\(` (left parenthesis)

75

- `)``\\)` (right parenthesis)

76

- `[``\\[` (left square bracket)

77

- `]``\\]` (right square bracket)

78

- `{``\\{` (left curly brace)

79

- `}``\\}` (right curly brace)

80

- `|``\\|` (pipe)

81

82

**Type Handling:**

83

- **Null/undefined**: Returns empty string

84

- **Symbols**: Converts to string representation

85

- **Numbers**: Converts to string (preserves sign of -0)

86

- **Other types**: Converts to string via implicit coercion

87

88

**Usage Examples:**

89

90

```javascript

91

// Basic escaping

92

escapeRegExp('hello.world');

93

// => 'hello\\.world'

94

95

// Multiple special characters

96

escapeRegExp('(test)*+?');

97

// => '\\(test\\)\\*\\+\\?'

98

99

// Null/undefined handling

100

escapeRegExp(null);

101

// => ''

102

103

escapeRegExp(undefined);

104

// => ''

105

106

// Empty string

107

escapeRegExp('');

108

// => ''

109

110

// Numbers

111

escapeRegExp(123);

112

// => '123'

113

114

// Preserves -0 sign

115

escapeRegExp(-0);

116

// => '-0'

117

118

// Complex patterns

119

escapeRegExp('[a-z]+\\.(js|ts)$');

120

// => '\\[a-z\\]\\+\\\\\\.(js\\|ts\\)\\$'

121

```