I have a URL string:
var url = /{{query}}/foo/{{query2}}
I have a line of code that is able to take in a string, then get an array of all the queries inside the braces:
var queries = String(url).match(/[^{\}]+(?=})/g);
Returns:
queries = ['query', 'query2']
I have a function, parse(queries)
, which processes these queries and returns a list of their results:
results = ['resultOfQuery', 'resultOfQuery2']
I want to be able to take this list, and then replace the queries in the URL string with their results. The final result of this example would be:
url =
I have two separate problems:
The regex in the
String.match
line of code only counts for once set of curly braces,{something}
. How can I modify it to look for a set of double curly braces, {{something}}?I already have the array of results. What is the best way to do the string replacement so that the queries and each of their acpanying set of double braces are replaced with their corresponding result?
I have a URL string:
var url = https://url./{{query}}/foo/{{query2}}
I have a line of code that is able to take in a string, then get an array of all the queries inside the braces:
var queries = String(url).match(/[^{\}]+(?=})/g);
Returns:
queries = ['query', 'query2']
I have a function, parse(queries)
, which processes these queries and returns a list of their results:
results = ['resultOfQuery', 'resultOfQuery2']
I want to be able to take this list, and then replace the queries in the URL string with their results. The final result of this example would be:
url = https://url./resultOfQuery/foo/resultOfQuery2
I have two separate problems:
The regex in the
String.match
line of code only counts for once set of curly braces,{something}
. How can I modify it to look for a set of double curly braces, {{something}}?I already have the array of results. What is the best way to do the string replacement so that the queries and each of their acpanying set of double braces are replaced with their corresponding result?
-
1
Try
var result = s.replace(/{{(.*?)}}/g, function($0,$1) { return parse($1); } )
, but yourparse
function should be able to accept a single string as argument. – Wiktor Stribiżew Commented Jun 21, 2019 at 18:27 -
1
What constraints are there on the format of the string between
{{ ... }}
? E.g. will it always be a valid JS variable name? – Gershom Maes Commented Jun 21, 2019 at 19:03 -
1
The string in between
{{ }}
can be any string. Myparse()
function does all the potential error handling. It could be{{foo}}
if the user was so inclined – erli Commented Jun 21, 2019 at 19:12
3 Answers
Reset to default 8You can use replace with following pattern,
{{(.+?)}}
{{
- Matches{{
(.+?)
- Matches anything except newline one or more time
let url = "https://url./{{query}}/foo/{{query2}}"
let result = {'query': 'queryResult1', 'query2':'queryResult2' }
let replaceDoubleBraces = (str,result) =>{
return str.replace(/{{(.+?)}}/g, (_,g1) => result[g1] || g1)
}
console.log(replaceDoubleBraces(url,result))
Note:- I am using result as object here so it bees easy to find and replace values, if you can change your parse function consider returning an object from parse
Generalized solution which will also work with nested object.
function replaceText(text, obj, start = '{{', end = '}}') {
return text.replace(new RegExp(`${start}(.+?)${end}`, 'g'), (_, part) => {
return part.split('.')
.reduce((o, k) => (
o || {}
)[k], obj);
});
}
console.log(replaceText(
'Hello my name is {{name.first}} {{name.last}}, age: {{age}}',
{
name: {
first: 'first', last: 'last'
},
age: 20,
}
));
To use Regex inbuild symbols as template, use double backslash \\
before each character. It is because above function uses template string new RegExp('\\^(.+?)\\^', 'g')
to create regular expression instead RegExp constructor new RegExp(/\^(.+?)\^/g)
, otherwise single backslash is enough.
For example
( => \\(
) => \\)
(( => \\(\\(
)) => \\)\\)
^ => \\^
Another simple way to do it:
const string = "Hello {{world}}";
console.log(string.replace(/{{.*}}/, "Stack Overflow"));