Here is a function that can be used to replace JavaScript variable name (object property, or array element) from string with its associated value defined in JavaScript, using RegExp.
// Function that replace token from string with equivalent variable value
// Receives the string-template. Returns that string with variable names replaced
function replaceStrVar(str){
 // JavaScript & jQuery Courses - https://coursesweb.net/javascript/
  str = str.replace(/%(.*?)%/gi, function(a,b) {
    // if token is an array item, else, is object property, or variable
    if(b.match(/[a-z0-9_]+\[[a-z0-9_]+\]/i)) {
      var arritm = b.match(/([a-z0-9_]+)\[([a-z0-9_]+)\]/i);      // gets an array with the matched items
      return window[arritm[1]][arritm[2]];
    }
    else {
      var voitm = b.split('.');
      return (voitm.length == 2) ? window[voitm[0]][voitm[1]] : window[voitm[0]];
    }
  });

  return str;
}
- The names for variable /array /object.property in the string must be added within %...% .

This function can be used to create a simple template-string in JavaScript.

Here's an example of replaceStrVar() function usage, using a variable, an array, and object, and a string (template) with their names that will be replaced with their values and the result is added into a HTML element.
<div id="testpl">String Template:<br/><span class="sbi">The website: &lt;b&gt;%website%&lt;/b&gt; contains: %obj.content1%, and it has around &lt;b&gt;%visits[2]%&lt;/b&gt; uniques visitors /day.</span></div>
<button onclick="testpl()">Test replaceStrVar()</button>

<script type="text/javascript"><!--
// Function that replace token from string with equivalent variable value
function replaceStrVar(str){
 // JavaScript & jQuery Courses - https://coursesweb.net/javascript/
  str = str.replace(/%(.*?)%/gi, function(a,b) {
    // if token is an array item, else, is object property, or variable
    if(b.match(/[a-z0-9_]+\[[a-z0-9_]+\]/i)) {
      var arritm = b.match(/([a-z0-9_]+)\[([a-z0-9_]+)\]/i);      // gets an array with the matched items
      return window[arritm[1]][arritm[2]];
    }
    else {
      var voitm = b.split('.');
      return (voitm.length == 2) ? window[voitm[0]][voitm[1]] : window[voitm[0]];
    }
  });

  return str;
}

  /* Test replaceStrVar() function */

var website = 'https://coursesweb.net';      // simple variable
var visits = new Array(100, 1000, 2000);        // array

// object
var obj = new Object();
 obj.content1 = 'Web Programming Courses';
 obj.content2 = 'Web Development Tutorials';

// The string (template)
var tpl = 'The website: <b>%website%</b> contains: %obj.content1%, and it has around <b>%visits[2]%</b> uniques visitors /day.';

// adds the replacement in #testpl HTML element
function testpl(){
  document.getElementById('testpl').innerHTML = replaceStrVar(tpl);
}
//-->
</script>
Results:
String Template:
The website: <b>%website%</b> contains: %obj.content1%, and it has around <b>%visits[2]%</b> uniques visitors /day.
- To see the result, click ->

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
Replace JavaScript variable name from string with its value

Last accessed pages

  1. Node.js Move and Copy file (3518)
  2. Get the value of the selected /checked checkboxes in a form (37630)
  3. Read Excel file data in PHP - PhpExcelReader (56711)
  4. Get and Modify content of an Iframe (12777)
  5. AJAX with POST and PHP (11915)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (448)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (352)
  3. Register and show online users and visitors (321)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (302)
  5. Insert, Select and Update NULL value in MySQL (214)