如何在 JavaScript 中替换 ASCII 码(alt+207)?

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

How to replace ASCII code (alt+207) in JavaScript?

javascriptregex

提问by Vivek

I have a string which contains square boxes(I found it's ascii code as alt+207)...

我有一个字符串,其中包含square boxes(我发现它是 ascii 代码alt+207)...

How can I replace this with ' '(a single space).

我怎样才能用''(一个空格)替换它。

回答by thomasrutter

I am assuming you mean the character: ¤

我假设您的意思是字符:¤

If so, you could try,

如果是这样,你可以试试,

str = str.replace('\u00A4', ' ');

Or if you want to do it to any character that is not ASCII, you could try something like:

或者,如果您想对任何非 ASCII 字符执行此操作,您可以尝试以下操作:

str = str.replace(/[^\u000A\u0020-\u007E]/g, ' ');

回答by Jason Gennaro

Is it one of these characters? ▪= ▪ or ■= ■

它是这些角色之一吗?▪= ▪ 或■= ■

If so, you can do this

如果是这样,你可以这样做

var str ="Something ▪ and ■";
str = str.replace(/▪|■/g, " ");
document.write(str);

http://jsfiddle.net/jasongennaro/8mCuV/

http://jsfiddle.net/jasongennaro/8mCuV/