CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash-reinterpolate

Internal lodash utility providing a regular expression for template interpolation delimiters

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

lodash._reinterpolate

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.

Package Information

  • Package Name: lodash._reinterpolate
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash._reinterpolate

Core Imports

var reInterpolate = require('lodash._reinterpolate');

For ES modules:

import reInterpolate from 'lodash._reinterpolate';

Basic Usage

var reInterpolate = require('lodash._reinterpolate');

// Use the regex to find template interpolation patterns
var template = 'Hello <%=name%>, you have <%=count%> messages.';
var matches = template.match(reInterpolate);

console.log(matches);
// Output: ['<%=name%>', '<%=count%>']

// Extract interpolation expressions
var expressions = [];
var match;
while ((match = reInterpolate.exec(template)) !== null) {
  expressions.push(match[1]); // Capture group contains the expression
}

console.log(expressions);
// Output: ['name', 'count']

Capabilities

Template Interpolation Pattern Matching

The module exports a single regular expression constant that matches ERB-style template interpolation delimiters.

/**
 * Regular expression for matching template interpolation delimiters.
 * Matches patterns like <%=expression%> and captures the expression content.
 * Pattern: /<%=([\s\S]+?)%>/g
 * - <%=      : Opening delimiter
 * - ([\s\S]+?) : Capture group for any characters including newlines (non-greedy)
 * - %>       : Closing delimiter  
 * - g        : Global flag for multiple matches
 */
var reInterpolate = /<%=([\s\S]+?)%>/g;

The regex pattern /<%=([\s\S]+?)%>/g specifically:

  • Matches the opening delimiter <%=
  • Captures any content including whitespace and newlines using [\s\S]+? (non-greedy)
  • Matches the closing delimiter %>
  • Uses the global flag g to find all matches in a string

Usage Examples:

var reInterpolate = require('lodash._reinterpolate');

// Simple interpolation matching
var simple = 'Welcome <%=user%>!';
console.log(simple.match(reInterpolate));
// Output: ['<%=user%>']

// Multiple interpolations
var multiple = 'Hello <%=firstName%> <%=lastName%>, you have <%=messageCount%> messages.';
var allMatches = multiple.match(reInterpolate);
console.log(allMatches);
// Output: ['<%=firstName%>', '<%=lastName%>', '<%=messageCount%>']

// Extract just the expressions using capture groups
var expressions = [];
var match;
// Reset regex lastIndex before using exec in a loop
reInterpolate.lastIndex = 0;
while ((match = reInterpolate.exec(multiple)) !== null) {
  expressions.push(match[1].trim());
}
console.log(expressions);
// Output: ['firstName', 'lastName', 'messageCount']

// Handle expressions with spaces and complex content
var complex = 'Result: <%=  user.name.toUpperCase()  %>';
var result = reInterpolate.exec(complex);
console.log(result[1]);
// Output: '  user.name.toUpperCase()  '
console.log(result[1].trim());
// Output: 'user.name.toUpperCase()'

// Multiline expressions (supported by [\s\S])
var multiline = `Template with <%=
  someFunction() +
  anotherValue
%> expression`;
reInterpolate.lastIndex = 0;
var multilineMatch = reInterpolate.exec(multiline);
console.log(multilineMatch[1]);
// Output includes the newlines and whitespace

Technical Details

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.

Key characteristics:

  • Zero dependencies: Pure JavaScript with no external requirements
  • Global matching: Use the g flag to find all interpolation patterns in a string
  • Capture groups: The parentheses capture the expression content without delimiters
  • Whitespace handling: Captures all whitespace as-is; trimming must be done separately
  • Newline support: [\s\S] character class includes newlines for multiline expressions
  • Non-greedy matching: +? ensures minimal matching to avoid capturing multiple expressions as one

Common patterns:

  • Use string.match(reInterpolate) to get all matches as an array
  • Use reInterpolate.exec() in a loop to access capture groups
  • Always reset reInterpolate.lastIndex = 0 before using exec() in loops
  • Use match[1].trim() to get clean expression content without surrounding whitespace

docs

index.md

tile.json