indiWiz.com
SiteSearch:
Jump:

Site Map | WizTools.org | jCraze Blog
Web Developer's Den!
Home : WDD : JavaScript
This Article...
Print Version
Add Comment
View Comments
JavaScript
The Bare Facts
Rollover Script
Script Windows
Status Message
Time Sensibility of JavaScript
JavaScript and Frames
Expandable Menus
Cursor Effects!
Regular Expressions Reference
JavaScript Screen Size
JavaScript HTMLize (Using Regular Expressions)
Java Script Dynamic Time Update
Sections

Commniquè
Sign Guestbook!
Read Guestbook!
MailMe!

JavaScript HTMLize (Using Regular Expressions)

Often we are faced with the task of converting HTML special characters. I always used to write this functionality in the server side. But having this functionality in the client side would save server's processing time. Also some functionality like a preview of text entered could be efficiently achieved by client side scripting. Now, the characters to be changed:

Special CharacterCorresponding HTML/XML
&&
<&lt;
>&gt;
"&quot;
\n<br/>\n

The JavaScript function to convert the same:


<script language="javascript">
function htmlize(str){
        str = str.replace(/\&/g,"&amp;");
        str = str.replace(/\</g,"&lt;");
        str = str.replace(/\>/g,"&gt;");
        str = str.replace(/\"/g,"&quot;");
        str = str.replace(/\n/g,"<br/>\n");
        return str;
}
</script>

Now would you like to check this functionality? Add some special characters, and click on the `HTMLize' button:



User Comments

Add Comment

[Quick Stats: Number of main threads: 3, Number of sub-threads: 1]


Sign Guestbook | Who is Subhash?
The contents of this site are copyright© 2000-2008, indiWiz.com. All Rights Reserved.