javascript替换特殊字符

use*_*820 3 javascript

是否可以使用javascript字符串替换功能翻译特殊字符,如®,ü等?

Mik*_*ace 5

当然是!

在Firebug控制台中运行它

"®ü".replace(/[®ü]/g,"replaced")
Run Code Online (Sandbox Code Playgroud)

"replacedreplaced"

你也可以

"®ü".replace(/[\xAE\xFC]/g,"Wohoo! ");
Run Code Online (Sandbox Code Playgroud)

返回

"Wohoo! Wohoo! "
Run Code Online (Sandbox Code Playgroud)

可以在http://www.ascii.cl/htmlcodes.htm找到一个好的十六进制符号查找页面


在这个页面上运行这个jQuery

$(".post-text").text().replace(/®/g," ******** ")
Run Code Online (Sandbox Code Playgroud)

回报

" is it possible to translate special characters like ******** , ü etc with javascript 
String replace function? Use this syntax... string.replace(/\xCC/g/,''); Where 'CC' is 
the hex character code for the char you are wanting to replace. In this example I am 
replacing with empty string ''. yes, and is as simple as can be: ' ******** '.replace('
 ******** ','anything'); Sure is! Running this in the Firebug console " ******** ü".
replace(/[ ******** ü]/g,"replaced") returned replacedreplaced "
Run Code Online (Sandbox Code Playgroud)

  • +1,如果你打算投票,至少给出一个理由.这个答案做了所要求的并给出了很好的例子.很彻底. (2认同)

sza*_*ata 2

是的,而且很简单:

\n\n
'\xc2\xae'.replace('\xc2\xae','anything');\n
Run Code Online (Sandbox Code Playgroud)\n