jQuery Html Friendly Characters

in jQuery I was wondering if there is a function that will convert the following string into
html friendly characters

var myString="@£¥®©®";

but using jquery it will convert that string to the following

@£¥®©®

So basically any Symbol in a string would get converted into the HTML friendly characters

Here is the chart below

http://www.ascii.cl/htmlcodes.htm
stargateatlantisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
>>in jQuery
no. try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script  type="text/javascript">
<!--
function start(){
	alert( friendly("a@b.com") )
}
 
function friendly(str)
{
	var searchFor="&,@,£,¥,®,©";
	var replaceWith="amp,pound,yen,reg,copy";
	
	searchFor=searchFor.split(",");
	replaceWith=replaceWith.split(",");
	
	var i=-1,limit=searchFor.length;
	while(++i<limit)
	{
		str=str.replace( new RegExp(searchFor[i],"g"), "&"+replaceWith[i]+";" );
	}
 
return str;
}
window.onload=start;
//-->
</script>
 
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stargateatlantisAuthor Commented:
But it would be great if you can replace any non numeric or alpha characters with html friendly
0
hieloCommented:
Not all of them have a "friendly" name, but all have a numeric code. Not sure why you want the friendly name, when the numeric entities will achive the same thing for a wider number of characters. IF you truly want the friendly names option then update the previous post:
        var searchFor="&,@,£,¥,®,©";
        var replaceWith="amp,pound,yen,reg,copy";

with the characters you want to replace. (BTW: remove @ from the searchFor list). Just to clarify, if you choose to go with the numeric codes, then £ would be converted to &#163; instead ot &pound;. If you were to work with xml, then &pound; would NOT be recognized automatically. You would need to declare it first. However, numeric entities work without having to take extra steps.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

brunoCommented:
try this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script  type="text/javascript">
<!--
function start(){
        alert( convert("a®b.com") )
}
 
 
function convert(str) {
	var selectedCharacters = str.split("");
	var transformedString = new Array();
 
	// loop over the characters in selected text
	for (i=0;i<selectedCharacters.length;i++) {
		// if ascii value of current character is greater than 127, then transform
		if (selectedCharacters[i].charCodeAt(0) > 127) {
			transformedString.push("&#"+selectedCharacters[i].charCodeAt(0)+";");
		} 
		// if ascii value of current character is less than 127, then leave as is
		else {
			transformedString.push(selectedCharacters[i]);
		}
 
	}
 
	// replace selected text with special-characters-translated text
	newStr = transformedString.join("");
	return newStr;
}
 
 
window.onload=start;
//-->
</script>
 
</body>
</html>

Open in new window

0
stargateatlantisAuthor Commented:
Can this be used with jQuery?
0
hieloCommented:
>>But it would be great if you can replace any non numeric or alpha characters with html friendly
If I understand you correctly, you want Letters and Numbers to be left alone, but encode everything else. If so, use:
function numeric(str)
{
	var i=-1,limit=str.length;
	var result="";
	while(++i<limit)
	{
		if( /[0-9a-zA-Z]/.test( str.charAt(i) ) )
		{
			result+=str.charAt(i)
		}
		else
		{
			result+= "&#"+str.charCodeAt(i)+";"
		}
	}
return result;
}

Open in new window

0
brunoCommented:
any javascript can be used with jquery. don't get hung up on jquery.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.