10 examples of 'jquery string format' in JavaScript

Every line of 'jquery string format' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
28var sprintf = function sprintf(str) {
29 var args = arguments,
30 flag = true,
31 i = 1;
32
33 str = str.replace(/%s/g, function () {
34 var arg = args[i++];
35
36 if (typeof arg === 'undefined') {
37 flag = false;
38 return '';
39 }
40 return arg;
41 });
42 return flag ? str : '';
43};
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

4function fmt(str, formats) {
5 var cachedFormats = formats;
6
7 if (!_.isArray(cachedFormats) || arguments.length > 2) {
8 cachedFormats = new Array(arguments.length - 1);
9
10 for (var i = 1, l = arguments.length; i < l; i++) {
11 cachedFormats[i - 1] = arguments[i];
12 }
13 }
14
15 // first, replace any ORDERED replacements.
16 var idx = 0; // the current index for non-numerical replacements
17 return str.replace(/%@([0-9]+)?/g, function(s, argIndex) {
18 argIndex = (argIndex) ? parseInt(argIndex, 10) - 1 : idx++;
19 s = cachedFormats[argIndex];
20 if (s === null) return '(null)';
21 if (s === undefined) return '';
22 if (_.isFunction(s.toString)) return s.toString();
23 return s;
24 });
25}
59export function sprintf(
60 // pattern containing %d formatters
61 format: string,
62 // value to replace
63 ...values): string {
64
65 let valueIndex = 0;
66 return format.replace(/%(0*)((?:[1-9])?)([dfx])(?:\^([rfcd]))?/g,
67 (_all, hasZeros, strNumDigits, specifier, roundFunc) => {
68 let out;
69 let strNumber;
70
71 let value = values[valueIndex++];
72
73 if (roundFunc) {
74 switch (roundFunc) {
75 case 'r': value = Math.round(value); break;
76 case 'd': value = downRound(value); break;
77 case 'c': value = Math.ceil(value); break;
78 case 'f': value = Math.floor(value); break;
79 }
80 }
81
82 switch (specifier) {
83 case 'd': strNumber = (roundFunc ? value : Math.floor(value)).toString(); break;
84 case 'f': strNumber = value.toString(); break;
85 case 'x': strNumber = value.toString(16); break;
86 }
87
88 out = strNumber;
89 if (strNumDigits) {
90 const numDigits = parseInt(strNumDigits);
91 if (numDigits > strNumber.length) {
92 out = new Array(numDigits - strNumber.length + 1).join(
93 hasZeros ? '0' : ' ') + out;
94 }
95 }
96 return out;
97 });
98}
23function Format(formatter, format) {
24 return undefined === format
25 ? func(formatter)
26 : regex(formatter, format);
27}
45export function format(format) {
46 const args = Array.prototype.slice.call(arguments, 1);
47 const sprintfRegex = /\{(\d+)\}/g;
48
49 function sprintf(match, number) {
50 return number in args ? args[number] : match;
51 }
52
53 return format.replace(sprintfRegex, sprintf);
54}
134format: function format(date) {
135 return new Date(date.toDateString() + ' 12:00:00 +0000').toISOString().substring(0, 10);
136},
19function format(str) {
20 return str;
21}
28exports[format] = function fmt(str) {
29 if (exports.no_color) {
30 return str;
31 }
32 return CSI + formats[format][0] + "m" + str + CSI + formats[format][1] + "m";
33};
55export function fmtString(s: string, param: string): string {
56 return s.replace(constant.STRING_FMT, param);
57}
28function strFormat(str) {
29 //------小数处理
30 var reg = /(^-?\d+)(\.\d+)$/; //如果带小数点的数字
31 if(reg.test(str)){
32 var int = reg.exec(str)[1]; //截取整数部分
33 var point = reg.exec(str)[2]; //截取小数部分
34
35 var res = int.replace(/(\d{1,2})(?=(\d{3})+$)/g,"$1, ");//对整数部分进行拆分
36 res += point;
37 }
38 //-----整数处理
39 var reg1 = /^-?\d+$/; //如果是整数
40 if(reg1.test(str)){
41 res = str.replace(/(\d{1,2})(?=(\d{4})+$)/g,"$1, "); //对整数部分进行拆分
42 res += '.00'; //末尾追加.00
43 }
44 return res;
45}

Related snippets