替换 |
interpolate | sub | scan | truncate | gsub |
interpolate: Treat the string as a template and populate it with the object's properties.
sub: Replace the first specified substrings in the string that match the pattern specified by pattern with replacement.
scan: Traverse all substrings in the string that match the pattern specified by the parameter pattern. Returns the original string itself.
truncate: Truncate the string to the specified length (including the suffix part) and add a suffix.
gsub: Replace all values in the string that match the pattern specified by pattern with replacement
Among the above methods, the most important one is gsub. For detailed instructions, see "A Brief Analysis of Prototype" Template class--Template》
sub is exactly the same as gsub except that it can limit the number of times.
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 the last thing scan returns is the string itself. .
interpolate uses strings as templates. The core is still gsub.
truncate is the only difference (I vaguely feel that I classified it in the wrong category now).
Taking the string 'fuck the gfw' as an example, the steps for truncate to execute 'fuck the gfw'.truncate(10,'****') are:
1. Get the first 10 - '** **'.length characters 'fuck t'
2. Add the suffix '****' to get 'fuck t****', the length is 10.
The processing is very simple, and the source code is also simple :
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: One of the conveniences of Prototype is that it can be extracted at any time Useful code as a separate section or collected for your own use. Below are the template methods proposed separately.
function Template(template, pattern){
this .template = template;
this.pattern = pattern || /(^|.|r|n)(#{(.*?)})/;
}
Template.prototype = (function (){
function evaluate(obj){
return gsub.call(this,function(match){
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
}
})();
var template = new Template('my age is : #{name.age}');
console.log(template.evaluate({name : {age : 24}})) ;//my age is : 24
String part (end)