JavaScript中實作sprintf、printf函數_javascript技巧
在 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"); 這樣用{?}來做匹配。

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

如何使用WebSocket和JavaScript實現線上語音辨識系統引言:隨著科技的不斷發展,語音辨識技術已成為了人工智慧領域的重要組成部分。而基於WebSocket和JavaScript實現的線上語音辨識系統,具備了低延遲、即時性和跨平台的特點,成為了廣泛應用的解決方案。本文將介紹如何使用WebSocket和JavaScript來實現線上語音辨識系

WebSocket與JavaScript:實現即時監控系統的關鍵技術引言:隨著互聯網技術的快速發展,即時監控系統在各個領域中得到了廣泛的應用。而實現即時監控的關鍵技術之一就是WebSocket與JavaScript的結合使用。本文將介紹WebSocket與JavaScript在即時監控系統中的應用,並給出程式碼範例,詳細解釋其實作原理。一、WebSocket技

如何利用JavaScript和WebSocket實現即時線上點餐系統介紹:隨著網路的普及和技術的進步,越來越多的餐廳開始提供線上點餐服務。為了實現即時線上點餐系統,我們可以利用JavaScript和WebSocket技術。 WebSocket是一種基於TCP協定的全雙工通訊協議,可實現客戶端與伺服器的即時雙向通訊。在即時線上點餐系統中,當使用者選擇菜餚並下訂單

如何使用WebSocket和JavaScript實現線上預約系統在當今數位化的時代,越來越多的業務和服務都需要提供線上預約功能。而實現一個高效、即時的線上預約系統是至關重要的。本文將介紹如何使用WebSocket和JavaScript來實作一個線上預約系統,並提供具體的程式碼範例。一、什麼是WebSocketWebSocket是一種在單一TCP連線上進行全雙工

JavaScript和WebSocket:打造高效的即時天氣預報系統引言:如今,天氣預報的準確性對於日常生活以及決策制定具有重要意義。隨著技術的發展,我們可以透過即時獲取天氣數據來提供更準確可靠的天氣預報。在本文中,我們將學習如何使用JavaScript和WebSocket技術,來建立一個高效的即時天氣預報系統。本文將透過具體的程式碼範例來展示實現的過程。 We

JavaScript教學:如何取得HTTP狀態碼,需要具體程式碼範例前言:在Web開發中,經常會涉及到與伺服器進行資料互動的場景。在與伺服器進行通訊時,我們經常需要取得傳回的HTTP狀態碼來判斷操作是否成功,並根據不同的狀態碼來進行對應的處理。本篇文章將教你如何使用JavaScript來取得HTTP狀態碼,並提供一些實用的程式碼範例。使用XMLHttpRequest

用法:在JavaScript中,insertBefore()方法用於在DOM樹中插入一個新的節點。這個方法需要兩個參數:要插入的新節點和參考節點(即新節點將要插入的位置的節點)。

JavaScript是一種廣泛應用於Web開發的程式語言,而WebSocket則是一種用於即時通訊的網路協定。結合二者的強大功能,我們可以打造一個高效率的即時影像處理系統。本文將介紹如何利用JavaScript和WebSocket來實作這個系統,並提供具體的程式碼範例。首先,我們需要明確指出即時影像處理系統的需求和目標。假設我們有一個攝影機設備,可以擷取即時的影像數
