Javascript Course

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
What attribute makes a radio button or checkbox input selected?
checked="checked" selected="selected" disabled="disabled"
<input type="checkbox" name="a_name" value="value" checked="checked" />
What CSS value scales the background image to the largest size contained within the element?
repeat-x contain linear-gradient
#id {
  background:url("path_to_image.png");
  background-size:contain;
  background-repeat:no-repeat;
}
What operator is used to determine the rest of the division of two numbers?
% * /
var rest8_7 = 8 % 7;
alert(rest8_7);
Indicate the PHP function that rounds a number up to the next highest integer.
floor() ceil() abs()
$nr = ceil(3.5);
echo $nr;        // 4
Replace JavaScript variable name from string with its value

Last accessed pages

  1. Send E-mail with HTML tags and Attachment (5591)
  2. Editing, Changing XML - E4X (1812)
  3. CSS Rhombus Shape (7618)
  4. PHP PDO - prepare and execute (9173)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (140494)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (69)
  2. Read Excel file data in PHP - PhpExcelReader (11)
  3. ActionScript 3 Lessons (7)
  4. The Mastery of Love (7)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (6)