简体   繁体   中英

javascript replacing special characters

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

Sure is!

Running this in the Firebug console

"®ü".replace(/[®ü]/g,"replaced")

returned

"replacedreplaced"

You can also do

"®ü".replace(/[\xAE\xFC]/g,"Wohoo! ");

which returns

"Wohoo! Wohoo! "

A good hex symbol lookup page can be found at http://www.ascii.cl/htmlcodes.htm


Example

running this jQuery on this page

$(".post-text").text().replace(/®/g," ******** ")

returns

" 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 "

是的,而且非常简单:

'®'.replace('®','anything');

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 ''.

Specifically for the characters listed in the question:

string.replace(/[\xAE\xFC]/g,'');

(yes, I know this question is old, but it still came up high in Google when I was looking for something specific, so the comment/answer might be helpful for some people):

Previous posters have told you how to replace single characters. I'd like to add that this is generally not a good idea (there are exceptions, of course), because there are so many possible "special characters" that you have to think of. You'll just end up adding more and more replacements and you'll still miss some.

It's generally easier and safer to look for one of the built-in functions to do this that will take of all characters that need special handling.

In your question, you don't tell us what you need it for - but I guess 3 years later, this is more for other people with similar problems anyway:

If you want to pass a string as a parameter, it's best to use the encodeURIComponent function. Also look at When are you supposed to use escape instead of encodeURI / encodeURIComponent? for that.

If you want to encode other things, it's always best to find a built-in function that does it for you. Maybe http://www.the-art-of-web.com/javascript/escape/ or http://shebang.mintern.net/foolproof-html-escaping-in-javascript/ (somewhat verbose, but with good explanation of things you should not do) will help

Use this JavaScript statement to replace all the special characters from a string

var a1 = $('#txt').val().replace(/[^\x30-\x39\x41-\x5A\x61-\x7A\x20\x26\x28\x29\x2C\x27\x22\x2E\x2F\x26\x40\x5C\x7C\x3A\x2D]/g, ''); 

The above statement will replace other than in the set 0-9A-Za-z &(),'"./&@\\|:- with a nothing ie deletes any control characters etc.

Still Better option is to replace all character(s) other than ASCII character set. ie

 var a1 = $('#txt').val().replace(/[^\x20-\x7E]/g, ''); 

ASCII character set starts from space ie \\x20 and ends on tilde ~ ie \\x7E. So replace all other characters; other than ASCII.

FYI: just in case if you don't know ^ sign is used indicate other than the set or character given.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM