[英]How to add | after every second number in string
Is there a regular expression to add this |有没有正则表达式来添加这个 | symbol after every second number in string.
字符串中每隔一个数字后的符号。
Have tried split join, filtering the string but with no luck已尝试拆分连接,过滤字符串但没有运气
let str1 = "Plymouth Belvedere 1968,1969 Plymouth GTX 1968,1969 Plymouth Road Runner 1968,1969 Plymouth Satellite 1968,1969"
Results should be结果应该是
Plymouth Belvedere 1968,1969|Plymouth GTX 1968,1969|Plymouth Road Runner 1968,1969|Plymouth Satellite 1968,1969|
You could look for decimals with comma and replace it with a pipe.您可以查找带逗号的小数并将其替换为 pipe。
let string = "Plymouth Belvedere 1968,1969 Plymouth GTX 1968,1969 Plymouth Road Runner 1968,1969 Plymouth Satellite 1968,1969", result = string.replace(/\d+,\d+/g, '$&|'); console.log(result);
let str1 = "Plymouth Belvedere 1968,1969 Plymouth GTX 1968,1969 Plymouth Road Runner 1968,1969 Plymouth Satellite 1968,1969";
let str2 = str1.replace(/\d+,\d+/g, "$&|");
str2 = str1.replace("| ", "|");
The first replace replaces numbers with the same numbers followed by a |
第一个替换用相同的数字替换数字,后跟
|
. . The second replace removes the spaces after
|
第二个替换删除
|
之后的空格. .
For the regular expression /\d+,\d+/g
, \d
matches all numbers, +
means that there is at least 1 digit but there can be more, ,
is for the comma that you have between the numbers, g
ensures that it iterates through the whole string, and $&
reinserts the matched string.对于正则表达式
/\d+,\d+/g
, \d
匹配所有数字, +
表示至少有 1 个数字,但可以有更多, ,
表示数字之间的逗号, g
确保它遍历整个字符串,然后$&
重新插入匹配的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.