Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将%放在数字后面的格式字符串_Javascript - Fatal编程技术网

Javascript 将%放在数字后面的格式字符串

Javascript 将%放在数字后面的格式字符串,javascript,Javascript,正在尝试找出如何将%附加到数字。数字长度不同,所以这就是我不确定的地方。如何创建一个正则表达式,它接受任何数字并在其后面附加% 我在想这个,但你会如何处理意外的长度 "\\%d{DigitlegnthVariesHere}" 还是像\\%d这样简单?下面是如何在字符串编辑中的每个整数后面加一个%的方法:请参见下面的小数: 例如: var s = "testing 123 testing 456"; s = s.replace(/\d+/g, "$&%"); console.log(s)

正在尝试找出如何将%附加到数字。数字长度不同,所以这就是我不确定的地方。如何创建一个正则表达式,它接受任何数字并在其后面附加%

我在想这个,但你会如何处理意外的长度

"\\%d{DigitlegnthVariesHere}"
还是像\\%d

这样简单?下面是如何在字符串编辑中的每个整数后面加一个%的方法:请参见下面的小数:

例如:

var s = "testing 123 testing 456";
s = s.replace(/\d+/g, "$&%");
console.log(s); // "testing 123% testing 456%"
var s = "testing 123.67 testing 456. And then...";
s = s.replace(/\d+(?:\.\d+)?/g, "$&%");
console.log(s); // "testing 123.67% testing 456%. And then..."
在下面的评论中,你说:

问题如果您输入47.56这样的小数点,它将输出45%56%

非常正确,因为\d仅代表数字,它不会神奇地包含

要处理小数,需要一个稍微复杂的表达式:

//                                    In the "search for" regular expression:
//             +--------------------- \d means "any digit"
//             | +------------------- +  means "one or more of the previous thing"
//             | |+------------------ (?:....) is a non-capturing group (more below)
//             | ||  +--------------- \. is a literal "."
//             | ||  | +------------- \d means "any digit" again
//             | ||  | |   +--------- ? means "zero or one of the previous thing,"
//             | ||  | |   |          which in this case is the non-capturing group
//             | ||  | |   |          containing (or not) the dot plus more digits
//             | ||  | |   | +------- The 'g' flag means "globally" in the string
//             | ||  | |   | |        --------------------------
//             | ||  | |   | |        In the replacement string:
//             | ||  | |   | |   +--- $& means "the text that matched"
//             | ||  | |   | |   | +- % isn't special here, it's just a literal % sign
//             | ||  | |   | |   | |
//             V VV  V V   V V   V V
s = s.replace(/\d+(?:\.\d+)?/g, "$&%");
所以基本上是这样说的:匹配一系列数字,可选地后跟一个小数点和更多的数字,然后用匹配的字符加上%来替换它们

例如:

var s = "testing 123 testing 456";
s = s.replace(/\d+/g, "$&%");
console.log(s); // "testing 123% testing 456%"
var s = "testing 123.67 testing 456. And then...";
s = s.replace(/\d+(?:\.\d+)?/g, "$&%");
console.log(s); // "testing 123.67% testing 456%. And then..."
请注意,尽管456后面紧跟着a,但因为它后面没有更多的数字,所以我们没有不适当地在..后面添加%

有一次你的问题是在数字前面而不是后面。如果您确实希望它位于数字前面,只需将%移到$&:

下面是如何在字符串编辑中的每个整数后加上一个%的方法:请参见下面的小数:

例如:

var s = "testing 123 testing 456";
s = s.replace(/\d+/g, "$&%");
console.log(s); // "testing 123% testing 456%"
var s = "testing 123.67 testing 456. And then...";
s = s.replace(/\d+(?:\.\d+)?/g, "$&%");
console.log(s); // "testing 123.67% testing 456%. And then..."
在下面的评论中,你说:

问题如果您输入47.56这样的小数点,它将输出45%56%

非常正确,因为\d仅代表数字,它不会神奇地包含

要处理小数,需要一个稍微复杂的表达式:

//                                    In the "search for" regular expression:
//             +--------------------- \d means "any digit"
//             | +------------------- +  means "one or more of the previous thing"
//             | |+------------------ (?:....) is a non-capturing group (more below)
//             | ||  +--------------- \. is a literal "."
//             | ||  | +------------- \d means "any digit" again
//             | ||  | |   +--------- ? means "zero or one of the previous thing,"
//             | ||  | |   |          which in this case is the non-capturing group
//             | ||  | |   |          containing (or not) the dot plus more digits
//             | ||  | |   | +------- The 'g' flag means "globally" in the string
//             | ||  | |   | |        --------------------------
//             | ||  | |   | |        In the replacement string:
//             | ||  | |   | |   +--- $& means "the text that matched"
//             | ||  | |   | |   | +- % isn't special here, it's just a literal % sign
//             | ||  | |   | |   | |
//             V VV  V V   V V   V V
s = s.replace(/\d+(?:\.\d+)?/g, "$&%");
所以基本上是这样说的:匹配一系列数字,可选地后跟一个小数点和更多的数字,然后用匹配的字符加上%来替换它们

例如:

var s = "testing 123 testing 456";
s = s.replace(/\d+/g, "$&%");
console.log(s); // "testing 123% testing 456%"
var s = "testing 123.67 testing 456. And then...";
s = s.replace(/\d+(?:\.\d+)?/g, "$&%");
console.log(s); // "testing 123.67% testing 456%. And then..."
请注意,尽管456后面紧跟着a,但因为它后面没有更多的数字,所以我们没有不适当地在..后面添加%

有一次你的问题是在数字前面而不是后面。如果您确实希望它位于数字前面,只需将%移到$&:


为了解释,+表示一个或多个前一个。它的限制性略高于*,这意味着前面的引号中的零或多个:在数字前面追加“%”。好答案。我知道前面的append在技术上是不正确的。@11684是的,但大多数人都说prepend@11684但这有什么关系?这并没有什么坏处——最好是安全的,在不完全清楚OP想要什么的时候,提供两种选择。T.J.解释了这两个词,所以它们的真正含义取决于OP的使用。@11684:我可以发誓,在第一个版本中,文本前面没有。要么我错过了,要么是忍者剪辑-为了解释,+表示一个或多个前一个。它的限制性略高于*,这意味着前面的引号中的零或多个:在数字前面追加“%”。好答案。我知道前面的append在技术上是不正确的。@11684是的,但大多数人都说prepend@11684但这有什么关系?这并没有什么坏处——最好是安全的,在不完全清楚OP想要什么的时候,提供两种选择。T.J.解释了这两个词,所以它们的真正含义取决于OP的使用。@11684:我可以发誓,在第一个版本中,文本前面没有。要么我错过了,要么是忍者剪辑-post错误,我的意思是在值之后。很抱歉。post错误,我的意思是在值之后。对不起。