JavaScript Based on Replace + Regulation to implement ES6 string templancular features

This example tells the JavaScript REPLACE + regular implementation of the string templan for ES6. Share to everyone for your reference, as follows:

In the form of a spliced ​​string, embed JSON data into HTML. At the beginning, the volume is less code, and it is still acceptable to temporarily. However, when the page structure is complicated, its weakness begins to be unbearable:

Writing is not coherent. Every time you write a variable, you will be broken into a + and “. It is very easy to errors.

The. HTML fragment is discrete data, it is difficult to extract the repeated part of it.

The label cannot be used well. This is a new label in HTML5. The standard is recommended to put the HTML template into the tag, making the code more concise.

Replace + regular implementation

Data stitching used by the project

  < arguments.length; i++) {
      var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
      str = str.replace(re, arguments[i]);
    }
    return str;
},

 Function FormatString () {if (arguments.length === 0) Return Null; var str = arguments [0]; for (var i = 1; i 

The first parameter is a data template that needs to be rendered, and other parameters are data:

 FormatString ('{0}, my name is {1}', 'Everyman', 'Wu Kongyun')   
This post maintenance Change, once the order is changed, you need to change ~~ If you need to receive a large string parameter, we try to write the form of an object,

 var myObject = maker (f , L, M, C, S); // Undequately Write Var myObject = Maker ({// Recommended Write First: f, Last: L, State: s, city: c});   
Search online Seeing an example, you can similar to the string template of ES6, the blog is a high school, stamp link.

Function Render (Template, Context) {// Subjects {and} should not be rendered, and there is a blank character between separators and variables. Var tokenreg = / (\\)? \ {([^ \ \ \ \} \\] +) (\\)? \} / g; return template.replace (Tokenreg, Function (Word, Slash2, Token, Slash3) {// If there is an escaped \ {or \} replace the escape character if (Slash2 || Slash3) {Return Word.Replace (‘\\’, ”);} // Cut Token, the Class-connected variable You can also expand VAR VARIABLES = token.replace (/ \ s / g, ‘) .split (‘. ‘); Var currentobject = context; var i, length, variable; for (i = 0, length = variables.length
  < length; ++i) {
      variable = variables[i];
      currentObject = currentObject[variable];
      // 如果当前索引的对象不存在,则直接返回空字符串。
      if (currentObject === undefined || currentObject === null) return '';
    }
    return currentObject;
  })
}
String.prototype.render = function (context) {
  return render(this, context);
};
"{greeting}! My name is { author.name }.".render({
  greeting: "Hi",
  author: {
    name: "hsfzxjy"
  }
});
// Hi! My name is hsfzxjy.

 
Expansion: ES6 string pattern API

ES6 introduces a new type of string word syntax, we Template string (Template strings).With the incecrace character`, instead of the quotation marks’ or “, they appear to have no two in the ordinary string.

VAR A = ‘Kenny’ `my name is $ {a}` // “My name is kenny”
can be compiled into ES5 with Bable
   PS: Here is a very convenient regular expression tool for everyone to use for your reference: 

JavaScript regular expression online test tool:

http:// Tools .JB51.NET / regex / javascript

Regular expression online generation tool:

http://tools.jb51.net/regex/create_reg
More about JavaScript-related content, readers who are interested in this site: “ECMAScript6 (ES6) Getting Started”, “JavaScript Regular Expression Skills”, “JavaScript Character and String Operation Skill Summary”, “JavaScript Data Structure and Algorithm Summary, “JavaScript error and debugging skill summary” and “Summary of JavaScript Mathematical Computing”

I hope this article will help you JavaScript programming.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment