Prototype source analysis string part (iv) of the supplementary _prototype

Source: Internet
Author: User
Replace Interpolate | Sub |  Scan | Truncate | Gsub
Interpolate: Treat a string as a template and populate it with the properties of object.
Sub: Replace with replacement the substring specified in the string that matches the pattern specified by
Scan: Iterates through all substrings in the string that match the pattern specified by the parameter patterns. Returns the original string itself.
Truncate: Truncate the string to the specified length (including the suffix portion) and add a suffix.
Gsub: Replaces all values in the string that match pattern-specified patterns with replacement

In the above method, one of the most important method is gsub, specifically see "Analysis of Prototype template class--template"
Sub can be limited to the number of times, other than gsub exactly the same.
Copy Code code as follows:

function sub (pattern, replacement, count) {
Replacement = preparereplacement (replacement);
Count = object.isundefined (count)? 1:count;
return this.gsub (pattern, function (match) {
if (--count < 0) return match[0];
Return replacement (match);
});
}

Scan is the same, but scan finally returns the string itself.
Interpolate is to use a string as a template, the core or gsub
Truncate is the only thing that's a little different (I now vaguely feel like I'm splitting the class).
Take the string ' Fuck the GFW ' For example, truncate's execution ' Fuck the GFW '. Truncate (10, ' * * * ') steps are:
1, get the front of the "* * * * * *". length character ' Fuck t '
2, spell the suffix ' * * * *, get ' fuck t**** ', length of 10.
The processing is very simple, the source code is also simple:
Copy Code code as follows:

function truncate (length, truncation) {
Length = Length | | 30;//Default Length 30
truncation = object.isundefined (truncation)? ' ... ': truncation;//default suffix ...
Return this.length > length?
This.slice (0, Length-truncation.length) + truncation:string (this);
}


Another: A handy thing about prototype is that you can always extract useful code as a separate part or for yourself. The following is a separate template approach.
Copy Code code as follows:

function Template (Template, pattern) {
This.template = template;
This.pattern = Pattern | | /(^|.| \r|\n) (#\{(. *?) \})/;
}
Template.prototype = (function () {
function evaluate (obj) {
Return Gsub.call (match) {This,function
if (obj = = null) {
return match[0] + ';
}
var before = match[1] | | '';
if (before = = ' \ ') {
return match[2];
}
var ctx = obj;
var expr = match[3];
var pattern =/^ ([^.[] +|\[((?:.*? [^\\])?) \])(\.| \[|$)/;
Match = pattern.exec (expr);
if (match = = null) {
return before;
}
while (match!= null) {
var comp = match[1].search (/^\[/)!=-1? match[2].replace (/\\\\]/g, '] '): match[1];
CTX = Ctx[comp];
if (null = = CTX | | ' = = match[3] ') break;
Expr = expr.substring (' [' = = match[3]? match[1].length:match[0].length);
Match = pattern.exec (expr);
}
Return before + (CTX = = null?) ": String (CTX));
});
}
function Gsub (replacement) {
var pattern = This.pattern;
var result = ';
var match = null;
var Source = this.template;
if (!) ( Pattern.length | | Pattern.source)) {
Replacement = replacement (");
Return replacement + source.split ('). Join (replacement) + replacement;
}
while (Source.length > 0) {
if (match = Source.match (pattern)) {
Result + = Source.slice (0, Match.Index);
Result + = replacement (match) = = null? ': String (Replacement (match));
Source = Source.slice (Match.Index + match[0].length);
}else {
result = = Source;
Source = ';
}
}
return result;
}
return {
Constructor:template,
Evaluate:evaluate
}
})();

Copy Code code as follows:

var template = new Template (' My Age is: #{name.age} ');
Console.log (Template.evaluate ({name: {age:24}}));//my Age is:24

String section (end)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.