从 JavaScript 中的字符串中删除长破折号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10436523/
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
Remove a long dash from a string in JavaScript?
提问by cd6
I've come across an error in my web app that I'm not sure how to fix.
我在我的网络应用程序中遇到了一个错误,我不知道如何修复。
Text boxes are sending me the long dash as part of their content (you know, the special long dash that MS Word automatically inserts sometimes). However, I can't find a way to replace it; since if I try to copy that character and put it into a JavaScript str.replace statement, it doesn't render right and it breaks the script.
文本框将长破折号作为其内容的一部分发送给我(您知道,MS Word 有时会自动插入特殊的长破折号)。但是,我找不到替换它的方法;因为如果我尝试复制该字符并将其放入 JavaScript str.replace 语句中,它不会正确呈现并且会破坏脚本。
How can I fix this?
我怎样才能解决这个问题?
The specific character that's killing it is —.
杀死它的特定角色是——。
Also, if it helps, I'm passing the value as a GET parameter, and then encoding it in XML and sending it to a server.
此外,如果有帮助,我会将值作为 GET 参数传递,然后将其编码为 XML 并将其发送到服务器。
回答by VisioN
This code might help:
此代码可能有帮助:
text = text.replace(/\u2013|\u2014/g, "-");
It replaces all–
(–) and —
(—) symbols with simple dashes (-).
它将所有–
(-) 和—
(-) 符号替换为简单的破折号 (-)。
回答by vcsjones
That character is call an Em Dash. You can replace it like so:
该角色称为Em Dash。您可以像这样替换它:
str.replace('\u2014', '');??????????
Here is an example Fiddle: http://jsfiddle.net/x67Ph/
这是一个示例小提琴:http: //jsfiddle.net/x67Ph/
The \u2014
is called a unicode escape sequence. These allow to to specify a unicode character by its code. 2014 happens to be the Em Dash.
这\u2014
称为unicode 转义序列。这些允许通过其代码指定一个 unicode 字符。2014 年恰好是 Em Dash。
回答by Trevor Norris
There are three unicode long-ish dashes you need to worry about: http://en.wikipedia.org/wiki/Dash
您需要担心三个 unicode 长破折号:http: //en.wikipedia.org/wiki/Dash
You can replace unicode characters directly by using the unicode escape:
您可以使用 unicode 转义直接替换 unicode 字符:
'—my string'.replace( /[\u2012\u2013\u2014\u2015]/g, '' )
回答by KooiInc
There may be more characters behaving like this, and you may want to reuse them in html later. A more generic way to to deal with it could be to replace all 'extended characters' with their html encoded equivalent. You could do that Like this:
可能会有更多的字符有这样的行为,您可能希望稍后在 html 中重用它们。处理它的更通用的方法可能是用它们的 html 编码等效替换所有“扩展字符”。你可以这样做:
[yourstring].replace(/[\u0080-\uC350]/g,
function(a) {
return '&#'+a.charCodeAt(0)+';';
}
);