Internal lodash utility providing a regular expression for template interpolation delimiters
npx @tessl/cli install tessl/npm-lodash--reinterpolate@3.0.00
# lodash._reinterpolate
1
2
lodash._reinterpolate is an internal lodash utility module that provides a regular expression for matching template interpolation delimiters. It exports a single RegExp constant used by lodash's template system to identify ERB-style interpolation expressions enclosed in `<%=` and `%>` tags.
3
4
## Package Information
5
6
- **Package Name**: lodash._reinterpolate
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash._reinterpolate`
10
11
## Core Imports
12
13
```javascript
14
var reInterpolate = require('lodash._reinterpolate');
15
```
16
17
For ES modules:
18
19
```javascript
20
import reInterpolate from 'lodash._reinterpolate';
21
```
22
23
## Basic Usage
24
25
```javascript
26
var reInterpolate = require('lodash._reinterpolate');
27
28
// Use the regex to find template interpolation patterns
29
var template = 'Hello <%=name%>, you have <%=count%> messages.';
30
var matches = template.match(reInterpolate);
31
32
console.log(matches);
33
// Output: ['<%=name%>', '<%=count%>']
34
35
// Extract interpolation expressions
36
var expressions = [];
37
var match;
38
while ((match = reInterpolate.exec(template)) !== null) {
39
expressions.push(match[1]); // Capture group contains the expression
40
}
41
42
console.log(expressions);
43
// Output: ['name', 'count']
44
```
45
46
## Capabilities
47
48
### Template Interpolation Pattern Matching
49
50
The module exports a single regular expression constant that matches ERB-style template interpolation delimiters.
51
52
```javascript { .api }
53
/**
54
* Regular expression for matching template interpolation delimiters.
55
* Matches patterns like <%=expression%> and captures the expression content.
56
* Pattern: /<%=([\s\S]+?)%>/g
57
* - <%= : Opening delimiter
58
* - ([\s\S]+?) : Capture group for any characters including newlines (non-greedy)
59
* - %> : Closing delimiter
60
* - g : Global flag for multiple matches
61
*/
62
var reInterpolate = /<%=([\s\S]+?)%>/g;
63
```
64
65
The regex pattern `/<%=([\s\S]+?)%>/g` specifically:
66
- Matches the opening delimiter `<%=`
67
- Captures any content including whitespace and newlines using `[\s\S]+?` (non-greedy)
68
- Matches the closing delimiter `%>`
69
- Uses the global flag `g` to find all matches in a string
70
71
**Usage Examples:**
72
73
```javascript
74
var reInterpolate = require('lodash._reinterpolate');
75
76
// Simple interpolation matching
77
var simple = 'Welcome <%=user%>!';
78
console.log(simple.match(reInterpolate));
79
// Output: ['<%=user%>']
80
81
// Multiple interpolations
82
var multiple = 'Hello <%=firstName%> <%=lastName%>, you have <%=messageCount%> messages.';
83
var allMatches = multiple.match(reInterpolate);
84
console.log(allMatches);
85
// Output: ['<%=firstName%>', '<%=lastName%>', '<%=messageCount%>']
86
87
// Extract just the expressions using capture groups
88
var expressions = [];
89
var match;
90
// Reset regex lastIndex before using exec in a loop
91
reInterpolate.lastIndex = 0;
92
while ((match = reInterpolate.exec(multiple)) !== null) {
93
expressions.push(match[1].trim());
94
}
95
console.log(expressions);
96
// Output: ['firstName', 'lastName', 'messageCount']
97
98
// Handle expressions with spaces and complex content
99
var complex = 'Result: <%= user.name.toUpperCase() %>';
100
var result = reInterpolate.exec(complex);
101
console.log(result[1]);
102
// Output: ' user.name.toUpperCase() '
103
console.log(result[1].trim());
104
// Output: 'user.name.toUpperCase()'
105
106
// Multiline expressions (supported by [\s\S])
107
var multiline = `Template with <%=
108
someFunction() +
109
anotherValue
110
%> expression`;
111
reInterpolate.lastIndex = 0;
112
var multilineMatch = reInterpolate.exec(multiline);
113
console.log(multilineMatch[1]);
114
// Output includes the newlines and whitespace
115
```
116
117
## Technical Details
118
119
This is a minimal utility module with zero dependencies, designed for maximum reusability. The regular expression is specifically crafted for lodash's internal template system but can be used independently for matching ERB-style interpolation patterns in any string.
120
121
**Key characteristics:**
122
- **Zero dependencies**: Pure JavaScript with no external requirements
123
- **Global matching**: Use the `g` flag to find all interpolation patterns in a string
124
- **Capture groups**: The parentheses capture the expression content without delimiters
125
- **Whitespace handling**: Captures all whitespace as-is; trimming must be done separately
126
- **Newline support**: `[\s\S]` character class includes newlines for multiline expressions
127
- **Non-greedy matching**: `+?` ensures minimal matching to avoid capturing multiple expressions as one
128
129
**Common patterns:**
130
- Use `string.match(reInterpolate)` to get all matches as an array
131
- Use `reInterpolate.exec()` in a loop to access capture groups
132
- Always reset `reInterpolate.lastIndex = 0` before using `exec()` in loops
133
- Use `match[1].trim()` to get clean expression content without surrounding whitespace