Today, in reading Qwrap source Stringh, there is a
Copy Code code as follows:
Format:function (S, arg0) {
var args = arguments;
Return S.replace (/\{(\d+) \}/ig, function (A, b) {
return args[(b | 0) + 1] | | '';
});
}
It is used in the following ways:
Alert (Format ("{0} love {1}.", ' I ', ' You '))//i
The format is implemented primarily by using the Replace method of a String object:
Replace: Returns a copy of the string that is replaced with text based on the regular expression.
1. Replace that is usually used
Copy Code code as follows:
function Replacedemo () {
var r, re; Declare a variable.
var ss = "The man hit the ball with the bat.\n";
SS = "While the fielder caught the ball with the glove."
re =/the/g; Creates a regular expression pattern.
r = Ss.replace (Re, "A"); Replace "the" with "A".
return (R); Returns the replaced string.
}
Replacedemo (); A Mans hit the ball with the bat. While the fielder caught is the ball with the glove.
2. subexpression in substitution mode
Copy Code code as follows:
function Replacedemo () {
var r, re; Declare a variable.
var ss = "The rain in Spain falls mainly in the plain.";
Re =/(\s+) (\s+) (\s+)/g; Creates a regular expression pattern.
r = Ss.replace (Re, "$3$2$1"); Exchange each pair of words.
return (R); Returns the result string.
}
document.write (Replacedemo ()); Rain the Spain in mainly falls the "in plain".
Matching regular items: The Rain,in spain,falls mainly,in the execution ss.replace (re, "$3$2$1") operation to complete the exchange of Word locations
The match is the first one (\s+)
The $ match is (\s+)
The $ $ match is the second one (\s+)
3.replace when the second parameter is a function
Copy Code code as follows:
function f2c (s) {
var test =/(\d+ (\.\d*)?) f\b/g; Initialization mode.
Return (S.replace test,function ($0,$1,$2) {return ((($1-32) + "C");});
}
F2C ("Water boils at 212F 3F. 2F 2.2F. 2");//water boils at 180c-29c. -30c-29.8c. 2
$ match 212f,3f,.2f,2.2f
Match 212,3,.2,2.2
$ match last. 2