I have:
window.page="home";
document.getElementsByTagName("title")[0].innerHTML=
"hello.com | $page".replace(/\$([^\s]*)/g,window["$1"]);
The idea is to get:
<title>hello.com | home</title>
But I instead get:
<title>hello.com | undefined</title>
When I do the following:
document.getElementsByTagName("title")[0].innerHTML=
"hello.com | $page".replace(/\$([^\s]*)/g,"$1");
I get:
<title>hello.com | page</title>
This is because the function is taking "$1" as an argument with which to map its output. I'm wondering if there is a way to use the capture group to access an array/object?
.replace(/\$(\S*)/g, function($0,$1) {return window[$1];})