I have this following problem. i try to split a string up depending on a regex.
I want to to split it by {{ name }}
, {{ age }}
and so on.
The expected output should look like this:
["hy my name is: ", "{{ name }}", " and i am ", "{{ age }}", " old"]
My current attempt was this here:
let str = "hy my name is: {{ name }} and i am {{ age }} old";
let vars = str.split(/({{)(.*)(}})/);
console.log(vars);
Whats the correct regex for this task?
.split(/({{.*?}})/)
– Wiktor Stribiżew Aug 6 '20 at 7:40{
and}
special characters? Why don't they need to be escaped? – npinti Aug 6 '20 at 7:41{6}
. In{.*?}
, they are parsed as literal chars. – Wiktor Stribiżew Aug 6 '20 at 7:42