在 JavaScript 下實作大多數語言中都有的 sprintf / printf 函數功能。
http://www.webtoolkit.info/javascript-sprintf.html : 比較完整的模擬sprintf函數功能。可用的格式化通配符:
1.%% - 回傳百分號本身
2.%b - 二進位數字
3.%c - ASCII對應的字元
4.%d - 整數
5.%f - 浮點數
6.%o - 八進位數字
7.%s - 字串
8.%x - 16進位數字 (小寫字母形式)
9.%X - 16進位數字 (大寫字母形式)
在 % 號和通配字元之間可用的選項包括 (例如 %.2f):
1. (強制顯示數字前面且 - 符號為正負數標記。缺省情況下只有負數才顯示 - 符號)
2.- (變項左對齊)
3.0 (使用0作為右對齊的填充字元)
4.[0-9] (設定變數的最小寬度)
5..[0-9] (設定浮點數精確度或字串的長度)
/**
*
* JavaScript sprintf
* http://www.webtoolkit.info/
*
*
**/
sprintfWrapper = {
init : 函數 () {
if (typeof argument == "undefined") { return null; }
if (arguments.length
if (typeof argument[0] != "string") { return null; } } }
if (typeof RegExp == "未定義") { return null; } }
var string = argument[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 (match[9]) { 轉換計數 = 1; }
stringPosStart = matchPosEnd;
stringPosEnd = exp.lastIndex - match[0].length;
strings[strings.length] = string.substring(stringPosStart, stringPosEnd);
matchPosEnd = exp.lastIndex;
匹配[匹配.長度] = {
匹配:匹配[0],
左:配對[3]?
符號:匹配[4] || '',
墊:匹配[5] || ' ',
分鐘:匹配[6] || 0,
精度:匹配[8],
代碼:匹配[9] || '%',
負數:parseInt(arguments[convCount])
參數:字串(參數[convCount])
};
}
strings[strings.length] = string.substring(matchPosEnd);
if ((arguments.length - 1)
var code = null;
var match = null;
var i = null;
if (matches[i].code == '%') { replacement = '%' }
else if (matches[i].code == 'b') {
matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
替換 = sprintfWrapper.convert(matches[i], true);
}
else if (matches[i].code == 'c') {
matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
替換 = sprintfWrapper.convert(matches[i], true);
}
else if (matches[i].code == 'd') {
matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
替換 = sprintfWrapper.convert(matches[i]);
}
else if (matches[i].code == 'f') {
matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i]. precision ? matches[i]. precision : 6));
替換 = sprintfWrapper.convert(matches[i]);
}
else if (matches[i].code == 'o') {
matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
替換 = sprintfWrapper.convert(matches[i]);
}
else if (matches[i].code == 's') {
matches[i].argument = matches[i].argument.substring(0, matches[i]. precision ? matches[i]. precision : matches[i].argument.length)
替換 = sprintfWrapper.convert(matches[i], true);
}
else if (matches[i].code == 'x') {
matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
替換 = sprintfWrapper.convert(matches[i]);
}
else if (matches[i].code == 'X') {
matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
替換 = sprintfWrapper.convert(matches[i]).toUpperCase();
}
否則{
替換 = matches[i].match;
}
newString = strings[i];
newString =替換;
}
newString = 字串[i];
return newString;
},
轉換:函數(符合、無符號){
如果(無符號){
match.sign = '';
}其他{
匹配.符號 = 匹配.負數 ? '-' : 匹配.sign;
}
var l = match.min - match.argument.length 1 - match.sign.length;
var pad = new Array(l
if (!match.left) {
if (match.pad == "0" || nosign) {
return match.sign pad match.argument;
}其他{
return pad match.sign match.argument;
}
}其他{
if (match.pad == "0" || nosign) {
return match.sign match.argument pad.replace(/0/g, ' ');
}其他{
return match.sign match.argument pad;
}
}
}
}
sprintf = sprintfWrapper.init;
如果只是想進行簡單的位置變數內容替換而不需要額外的格式化處理的話,可以用比較簡單的 YUI tools 中所提供的printf:
YAHOO.Tools.printf = function() {
var num = arguments.length;
var oStr = arguments[0];
for (var i = 1; i var pattern = "\{" (i-1) "\}";
var re = new RegExp(pattern, "g");
oStr = oStr.replace(re, arguments[i]);
}
return oStr;
}
使用的時候像 YAHOO.Tools.printf("顯示字串 {0} , {1}。", "1", "2"); 這樣用{?}來做匹配。

是的,JavaScript的引擎核心是用C語言編寫的。 1)C語言提供了高效性能和底層控制,適合JavaScript引擎的開發。 2)以V8引擎為例,其核心用C 編寫,結合了C的效率和麵向對象特性。 3)JavaScript引擎的工作原理包括解析、編譯和執行,C語言在這些過程中發揮關鍵作用。

JavaScript是現代網站的核心,因為它增強了網頁的交互性和動態性。 1)它允許在不刷新頁面的情況下改變內容,2)通過DOMAPI操作網頁,3)支持複雜的交互效果如動畫和拖放,4)優化性能和最佳實踐提高用戶體驗。

C 和JavaScript通過WebAssembly實現互操作性。 1)C 代碼編譯成WebAssembly模塊,引入到JavaScript環境中,增強計算能力。 2)在遊戲開發中,C 處理物理引擎和圖形渲染,JavaScript負責遊戲邏輯和用戶界面。

JavaScript在網站、移動應用、桌面應用和服務器端編程中均有廣泛應用。 1)在網站開發中,JavaScript與HTML、CSS一起操作DOM,實現動態效果,並支持如jQuery、React等框架。 2)通過ReactNative和Ionic,JavaScript用於開發跨平台移動應用。 3)Electron框架使JavaScript能構建桌面應用。 4)Node.js讓JavaScript在服務器端運行,支持高並發請求。

Python更適合數據科學和自動化,JavaScript更適合前端和全棧開發。 1.Python在數據科學和機器學習中表現出色,使用NumPy、Pandas等庫進行數據處理和建模。 2.Python在自動化和腳本編寫方面簡潔高效。 3.JavaScript在前端開發中不可或缺,用於構建動態網頁和單頁面應用。 4.JavaScript通過Node.js在後端開發中發揮作用,支持全棧開發。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

記事本++7.3.1
好用且免費的程式碼編輯器