Implémentez la fonction Sprintf / printf disponible dans la plupart des langues sous JavaScript.
http://www.webtoolkit.info/javascript-sprintf.html: une simulation relativement complète des fonctions de fonction sprintf. Les caractères génériques à formater:
1. %% - Retour au pourcentage de signe lui-même
2.% B - Numéro binaire
3.% C - Caractères correspondants ASCII
4.% D - entier
5.% F - Numéro de point flottant
6.% O - Nombre octal
7.% S - chaîne
8.% x - numéro hexadécimal (formulaire de lettre minuscule)
9.% x - chiffres hexadécimaux (formulaire de lettre capsulaire)
Les options disponibles entre le panneau% et les caractères génériques incluent (par exemple,% .2f):
1. + (forçant les symboles + et - à afficher devant le nombre en tant que nombres positifs et négatifs. Par défaut, seuls les nombres négatifs seront affichés dans le symbole))
2.- (variable gauche alignée)
3.0 (utilisez 0 comme caractère de rembourrage aligné droit)
4. [0-9] (Définissez la largeur minimale de la variable)
5 .. [0-9] (Définissez la précision du point flottante ou la longueur de la chaîne)
La copie de code est la suivante:
/ **
*
* Sprintf javascript
* http://www.webtoolkit.info/
*
*
** /
sprintfwrapper = {
init: function () {
if (typeof arguments == "Undefined") {return null; }
if (arguments.length <1) {return null; }
if (typeof arguments [0]! = "String") {return null; }
if (typeof regexp == "Undefined") {return null; }
var string = arguments [0];
var exp = new regexp (/ (% ([%] | (/ -)? (/ + | / x20)? (0)? (/ d +)? (/. (/ d)?)? ([bcdfosxx]))) / g);
var matches = new Array ();
var strings = new Array ();
var convcount = 0;
var stringPosstart = 0;
var stringPosend = 0;
var matchPoSend = 0;
var newstring = '';
var match = null;
while (match = exp.exec (string)) {
if (correspond [9]) {convCount + = 1; }
StringPosstart = MatchPoSend;
StringPosend = Exp.LastIndex - Match [0] .Length;
Strings [Strings.Length] = String.SubString (StringPosStart, StringPosend);
MatchPoSend = Exp.LastIndex;
correspond à [Matches.Length] = {
Match: Match [0],
Gauche: Match [3]? vrai: faux,
Signe: Match [4] || '',
Pad: Match [5] || '',
Min: Match [6] || 0,
précision: correspondant [8],
Code: correspondant [9] || '%',
négatif: parseInt (arguments [convcount]) <0? vrai: faux,
Argument: String (arguments [convcount])
};
}
Strings [Strings.Length] = String.SubString (MatchPosend);
if (correspond.length == 0) {return String; }
if ((arguments.length - 1) <convcount) {return null; }
var code = null;
var match = null;
var i = null;
pour (i = 0; i <matches.length; i ++) {
if (correspond [i] .code == '%') {substitution = '%'}
else if (correspond [i] .code == 'b') {
correspond [i] .argument = string (math.abs (parseInt (correspond [i] .argument)). toString (2));
substitution = sprintfwrapper.convert (correspond [i], true);
}
else if (correspond [i] .code == 'c') {
correspond [i] .argument = string (string.fromCharcode (parseInt (math.abs (parseInt (correspond [i] .argument)))));
substitution = sprintfwrapper.convert (correspond [i], true);
}
else if (correspond [i] .code == 'd') {
correspond [i] .argument = string (math.abs (parseInt (correspond [i] .argument)));
substitution = sprintfwrapper.convert (correspond [i]);
}
else if (correspond [i] .code == 'f') {
correspond [i] .argument = string (math.abs (parsefloat (correspond [i] .argument)). tofixed (correspond [i] .précision? correspond [i] .précision: 6));
substitution = sprintfwrapper.convert (correspond [i]);
}
else if (correspond [i] .code == 'o') {
correspond [i] .argument = string (math.abs (parseInt (correspond [i] .argument)). toString (8));
substitution = sprintfwrapper.convert (correspond [i]);
}
else if (correspond [i] .code == 's') {
correspond [i] .argument = correspond [i] .argument.substring (0, correspond [i] .précision? correspond [i] .précision: correspond [i] .argument.length)
substitution = sprintfwrapper.convert (correspond [i], true);
}
else if (correspond [i] .code == 'x') {
correspond [i] .argument = string (math.abs (parseInt (correspond [i] .argument)). toString (16));
substitution = sprintfwrapper.convert (correspond [i]);
}
else if (correspond [i] .code == 'x') {
correspond [i] .argument = string (math.abs (parseInt (correspond [i] .argument)). toString (16));
substitution = sprintfwrapper.convert (correspond [i]). touppercase ();
}
autre {
substitution = correspond aux correspondances [i] .match;
}
NewString + = Strings [i];
Newsring + = substitution;
}
NewString + = Strings [i];
retour de la nouvelle;
},
converti: fonction (match, nosign) {
if (nosign) {
match.sign = '';
} autre {
match.sign = match.negative? '-': match.sign;
}
var l = match.min - match.argument.length + 1 - match.sign.length;
var pad = nouveau tableau (l <0? 0: l) .join (match.pad);
if (! Match.left) {
if (match.pad == "0" || nosign) {
return match.sign + pad + match.argument;
} autre {
return pad + match.sign + match.argument;
}
} autre {
if (match.pad == "0" || nosign) {
return match.sign + match.argument + pad.replace (/ 0 / g, '');
} autre {
return match.sign + match.argument + pad;
}
}
}
}
sprintf = sprintfwrapper.init;
Si vous souhaitez simplement remplacer le contenu de la variable de position sans formatage supplémentaire, vous pouvez utiliser l'imprimé fourni dans les outils YUI plus simples:
La copie de code est la suivante:
Yahoo.tools.printf = fonction () {
var num = arguments.length;
var ostr = arguments [0];
pour (var i = 1; i <num; i ++) {
var pattern = "// {" + (i-1) + "//}";
var re = new regexp (modèle, "g");
ostr = ostr.replace (re, arguments [i]);
}
retour OSTR;
}
Lorsque vous l'utilisez, utilisez {?} Pour le faire correspondre.