Javascript Regex:用逗号替换最后一个点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4843691/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Regex: replacing the last dot for a comma
提问by Shyam
I have the following code:
我有以下代码:
var x = "100.007"
x = String(parseFloat(x).toFixed(2));
return x
=> 100.01
This works awesomely just how I want it to work. I just want a tiny addition, which is something like:
这非常有效,正是我希望它工作的方式。我只想要一个小小的补充,就像:
var x = "100,007"
x.replace(",", ".")
x.replace
x = String(parseFloat(x).toFixed(2));
x.replace(".", ",")
return x
=> 100,01
However, this code will replace the first occurrence of the ",", where I want to catch the last one. Any help would be appreciated.
但是,此代码将替换第一次出现的“,”,我想在其中捕获最后一个。任何帮助,将不胜感激。
回答by Pointy
You can do it with a regular expression:
您可以使用正则表达式来做到这一点:
x = x.replace(/,([^,]*)$/, ".");
That regular expression matches a comma followed by any amount of text not including a comma. The replacement string is just a period followed by whatever it was that came after the original last comma. Other commas preceding it in the string won't be affected.
该正则表达式匹配一个逗号,后跟任意数量的文本,不包括逗号。替换字符串只是一个句点,后跟原始最后一个逗号之后的任何内容。字符串中它前面的其他逗号不会受到影响。
Now, if you're really converting numbers formatted in "European style" (for lack of a better term), you're also going to need to worry about the "." characters in places where a "U.S. style" number would have commas. I think you would probably just want to get rid of them:
现在,如果你真的要转换格式为“欧洲风格”的数字(因为没有更好的术语),你还需要担心“。” 在“美国风格”数字有逗号的地方的字符。我想你可能只想摆脱它们:
x = x.replace(/\./g, '');
When you use the ".replace()" function on a string, you should understand that it returnsthe modified string. It does not modify the originalstring, however, so a statement like:
当您在字符串上使用“.replace()”函数时,您应该明白它返回修改后的字符串。但是,它不会修改原始字符串,因此语句如下:
x.replace(/something/, "something else");
has no effect on the value of "x".
对“x”的值没有影响。
回答by Mike Dinescu
You could do it using the lastIndexOf()function to find the last occurrence of the ,and replace it.
您可以使用lastIndexOf()函数查找最后一次出现的,并替换它。
The alternative is to use a regular expression with the end of line marker:
另一种方法是使用带有行尾标记的正则表达式:
myOldString.replace(/,([^,]*)$/, ".");
回答by GolezTrol
You can use lastIndexOfto find the last occurence of ,. Then you can use sliceto put the part before and after the ,together with a .inbetween.
您可以使用lastIndexOf来查找 的最后一次出现,。然后您可以使用slice将前后的部分放在,一起,.中间加上一个中间部分。
回答by p4bl0
You can use a regexp. You want to replace the last ',', so the basic idea is to replace the ',' for which there's no ',' after.
您可以使用正则表达式。您想替换最后一个 ',',因此基本思想是替换后面没有 ',' 的 ','。
x.replace(/,([^,]*)$/, ".");
Will return what you want :-).
会返回你想要的:-)。
回答by coreyward
You don't need to worry about whether or not it's the last ".", because there is only one. JavaScript doesn't store numbers internally with comma or dot-delimited sets.
您不必担心它是否是最后一个“.”,因为只有一个。JavaScript 不会在内部使用逗号或点分隔集存储数字。

