Jerminix

"Ramblings in Life Events On and Offline"

May 15th, 2009 | by | javascript, web

May
15

Convert special characters to any string you want using replace() function in javascript. Useful when converting characters to be saved in the database or passed as a parameter in a url.

function convertSC(oldstring)
{
newstring = oldstring.replace(/”/g, “_DQ_”); //double quote
newstring = newstring.replace(/’/g, “_SQ_”); //apostrophe
newstring = newstring.replace(/&/g, “_@_”); //ampersand
newstring = newstring.replace(/%/g, “_PC_”); //percentage

return newstring;
}

Example:
var convertme = convertSC(“hello world and hello universe”);
alert(converme);

Output:
hello world _@_ hello universe

Related Posts Plugin for WordPress, Blogger...

No related posts.

Authored by

A Pinoy Cebu based blogger, Gamer, Web Developer, Event Wanderer and wanna be photographer blogging about everything and anything under the sun.

Leave a Comment

CommentLuv badge

Featuring Recent Posts WordPress Widget development by YD