javascript ersätter specialtecken
är det möjligt att översätta specialtecken som ®, ü etc med JavaScript String replace-funktionen?
Svar #1
Visst är det!
Kör detta i Firebug-konsolen
"®ü".replace(/[®ü]/g,"replaced")
återvände
"ersattersatt"
Du kan också göra
"®ü".replace(/[\xAE\xFC]/g,"Wohoo! ");
som returnerar
"Wohoo! Wohoo! "
En bra hexsymboluppslagsida finns på http://www.ascii.cl/htmlcodes.htm
Exempel
köra den här jQuery på den här sidan
$(".post-text").text().replace(/®/g," ******** ")
returnerar
" 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 "
Svar #2
ja, och är så enkelt som det kan bli:
'®'.replace('®','anything');
