javascript - 将 % 放在数字后的格式字符串

标签 javascript

试图找出如何将 % 附加到数字。数字长度各不相同,所以我不确定......如何创建一个正则表达式来获取任何数字并将 % 附加到它。

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

"\\%d{DigitlegnthVariesHere}"

或者它是像 "\\%d" 这样简单的东西吗?

最佳答案

下面是如何在字符串中的每个 (整数) 数字后放置一个 % (编辑:请参阅下面的小数部分):

//                              In the "search for" regular expression:
//             +--------------- \d means "any digit"
//             | +------------- +  means "one or more of the previous thing"
//             | | +----------- 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 V V   V V
s = s.replace(/\d+/g, "$&%");

例子:

var s = "testing 123 testing 456";
s = s.replace(/\d+/g, "$&%");
console.log(s); // "testing 123% testing 456%"

在下面的评论中,您说:

problem if you enter a decimal like 47.56, it'll output 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.67 testing 456. And then...";
s = s.replace(/\d+(?:\.\d+)?/g, "$&%");
console.log(s); // "testing 123.67% testing 456%. And then..."

请注意,即使 456 后面跟着一个 .,因为它后面没有更多的数字,所以我们没有不恰本地添加一个 >%..

之后

在某一时刻,您的问题说的是数字“前面”而不是后面。如果你真的想要它在数字的前面,只需将 % 移到 $& 之前:

str = str.replace(/\d+/g, "%$&");

关于javascript - 将 % 放在数字后的格式字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16652164/

相关文章:

javascript - 通过 URL 传递参数

javascript - 带暂停和恢复轮询的 Rxjs 计时器

javascript - 验证电子邮件或电话号码

javascript - 添加过渡到 Zoomable Circle Packing

javascript - 如何将函数设置为事件监听器

javascript - 根据条件改变D3散点图上多个点的半径

javascript - 在 $.ajax 中发送键值数组

javascript - 如何解析 Objective-C 中 JavaScript 赋值语句中的值

javascript - 使用 Javascript 将 Div 的背景颜色更改为变量的值

javascript - 如何在PHP中不使用cookie或JS将数据发送到另一个页面?