你如何替换 javascript 中的英镑(货币)符号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7418819/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:03:43  来源:igfitidea点击:

How do you replace a pound (currency) sign in javascript?

javascript

提问by Lee

I'm trying to remove a pound sign (£) from a string using javascript. I'm trying to do it using

我正在尝试使用 javascript 从字符串中删除井号 (£)。我正在尝试使用

str = str.replace(/\£/g, "");

However, it is not removing the sign.

但是,它并没有删除标志。

The value of str is being fetched from a span (and the correct value is being fetched). This span has been previously set using javascript, with it being encoded in the string as

正在从跨度中获取 str 的值(并且正在获取正确的值)。这个跨度以前是使用 javascript 设置的,它在字符串中被编码为

£

Any ideas on the best way to remove the pound sign?

关于删除英镑符号的最佳方法的任何想法?

回答by jmar777

You may need to use unicode for this. E.g., '£10.00'.replace(/\u00A3/g, '');

您可能需要为此使用 unicode。例如,'£10.00'.replace(/\u00A3/g, '');

回答by Matthew Wilson

Remove the backslash from your regexp.

从正则表达式中删除反斜杠。

回答by Nicola Peluchetti

This way it works for me:

这样它对我有用:

var str = "£sdfsdf";

str = str.replace("£", "");

alert(str);

Fiddle here: http://jsfiddle.net/peUrn/1/

在这里小提琴:http: //jsfiddle.net/peUrn/1/

回答by nidhin

The easy way is:

简单的方法是:

str.replace('£', '');

回答by Joseph Marikle

You can just do

你可以做

"hello w£orld".replace(/£/g,"")

回答by Todd Moses

Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character

通过用一个、两个或三个表示字符的 UTF-8 编码的转义序列替换某些字符的每个实例,对统一资源标识符 (URI) 组件进行编码

Which means, in order to encode a pound sign, JavaScript uses 2 characters.

这意味着,为了对井号进行编码,JavaScript 使用 2 个字符。

£ = %C2%A3

See http://fyneworks.blogspot.com/2008/06/british-pound-sign-encoding-revisited.htmlfor more information.

有关更多信息,请参阅http://fyneworks.blogspot.com/2008/06/british-pound-sign-encoding-revisited.html

It would be best to use %C2%A3 in place of the pound sign in your script.

最好使用 %C2%A3 代替脚本中的井号。