EN
JavaScript - insert variable into template text with regular expression
11 points
In this article, we want to show how in a simple way, to use string templates in JavaScript.
The article contains 3 approaches:
- simple custom template logic,
- string replace based approach,
- templates literals (provided by modern JavaScript).
This solution finds matching variables in the text and replaces them with desired values. Variables in the text are marked with curly brackets (e.g. {variable_name_here}
).
xxxxxxxxxx
1
function escapePattern(text) {
2
return text.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
3
}
4
5
function renderVariable(text, name, value) {
6
var pattern = escapePattern(name);
7
var expression = new RegExp('{' + pattern + '}', 'g');
8
return text.replace(expression, value);
9
}
10
11
function renderText(text, variables) {
12
var result = text;
13
for (var i = 0; i < variables.length; ++i) {
14
var variable = variables[i];
15
result = renderVariable(result, variable.name, variable.value);
16
}
17
return result;
18
}
19
20
21
// Usage example:
22
23
var text =
24
'This is {name} profile.\n' +
25
'Details:\n' +
26
' name: {name}\n' +
27
' email: {email}\n' +
28
' age: {age}\n' +
29
'Contact with {name} by {email}.';
30
31
var variables = [
32
{name: 'name', value: 'John Bean' },
33
{name: 'email', value: 'john.bean@dirask.com'},
34
{name: 'age', value: '53' }
35
];
36
37
var renderedText = renderText(text, variables);
38
39
console.log('-- BEFORE --\n' + text + '\n\n');
40
console.log('-- AFTER --\n' + renderedText + '\n\n');
This solution just replaces variables in the text using function logic.
xxxxxxxxxx
1
function renderText(template, variables) {
2
return template.replace(/\{([0-9a-zA-Z_-]+)\}/g, function(match, variable) {
3
return variables[variable];
4
});
5
}
6
7
8
// Usage example:
9
10
var template =
11
'This is {name} profile.\n' +
12
'Details:\n' +
13
' name: {name}\n' +
14
' email: {email}\n' +
15
' age: {age}\n' +
16
'Contact with {name} by {email}.';
17
18
var variables = {
19
name : 'John Bean',
20
email : 'john.bean@dirask.com',
21
age : '53'
22
};
23
24
var renderedText = renderText(template, variables);
25
26
console.log('-- BEFORE --\n' + template + '\n\n');
27
console.log('-- AFTER --\n' + renderedText + '\n\n');
To find matches /\{([0-9a-zA-Z]+)\}/g
expression has been used.
Where:
/.../g
- regular expression with global searching (all occurrences)\{...\}
- matches only text bounded with{ }
(...)
- group content and replace it with a variable:function(match, variable) { ... }
[0-9a-zA-Z]+
- one or more occurrences of letters or numbers
That approach has a second name like Template strings too. In other languages, we can find substitute names like Expression interpolation or String interpolation.
xxxxxxxxxx
1
var name = 'John Bean';
2
var email = 'john.bean@dirask.com';
3
var age = 53;
4
5
var text =
6
`This is ${name} profile.
7
Details:
8
name: ${name}
9
email: ${email}
10
age: ${age}
11
Contact with ${name} by ${email}.`;
12
13
console.log(text);