473,734 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Escape/ Unescape HTML?

How do I convert a string to escaped HTML? (Let's say I'm not using
DOM.) E.g. "<p>foo</p>" would become "&lt;p&gt;foo&l t;/p&gt;"?

Reversely, how do I unescape a string to be HTML? E.g. "&lt;em&gt; "
would turn back into "<em>".

I mean a function like the following would do the job but is there a
more JS-native solution?

html = html.replace(/&/g, "&amp;");
html = html.replace(/</g, "&lt;");
html = html.replace(/>/g, "&gt;");
html = html.replace(/"/g, "&quot;");

Thanks!
Dec 20 '07 #1
1 7630
Philipp wrote on 20 dec 2007 in comp.lang.javas cript:
How do I convert a string to escaped HTML? (Let's say I'm not using
DOM.) E.g. "<p>foo</p>" would become "&lt;p&gt;foo&l t;/p&gt;"?

Reversely, how do I unescape a string to be HTML? E.g. "&lt;em&gt; "
would turn back into "<em>".
I mean a function like the following would do the job but is there a
more JS-native solution?
Ever tried asking the natives? Oh you just did. ;-)

s = unescape(s);

the below is not unescape, but escape-ish:
html = html.replace(/&/g, "&amp;");
html = html.replace(/</g, "&lt;");
html = html.replace(/>/g, "&gt;");
html = html.replace(/"/g, "&quot;");
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 20 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7734
by: Daniel | last post by:
I'm working with strings that contain xml escape codes, such as '0' and need a way in python to unescape these back to their ascii representation, such as '&' but can't seem to find a python method for this. I tried xml.sax.saxutils.unescape(s), but while it works with '&amp;', it doesn't work with '0' and other numeric codes. Any suggestions on how to decode the numeric xml escape codes such as this? Thanks. -- To reply to me directly,...
6
5019
by: Paul Watson | last post by:
How can I get the escapes from a command line parameter interpreted? The user provides a string on the command line. The string might contain traditional escapes such as \t, \n, etc. It might also contain escaped octal or hex such as \001 or \x09. The escapes are coming into sys.argv without shell interpretation. Do I need to use the compile module to make this work? Any suggestions? ===
3
7300
by: harrelson | last post by:
I have a list of about 2500 html escape sequences (decimal) that I need to convert to utf-8. Stuff like: 비 행 기 로 보 낼 거
1
19540
by: JigMan | last post by:
When chucking data in a cookie you cannot use certain characters (in this example ‘ : ‘ ). To get around this you use escape and unescape. Trying to use the escape as such: Response.Cookies("Lines")("URL4") = escape("http://www.ASP101.com") Gives the following result:
2
14478
by: Pavils Jurjans | last post by:
Hello, I am looking fow C# equivalent of JavaScripts escape() and unescape() functions. I need to use C# function at the server side and then be able to use the opposite at the client side. JavaScripts escape() function converts all non-alphanumerical chars to %xx, and all unicode chars to %uxxxx (where xxxx is unicode charcode). I'd like to have C# function that get this escaped string back to normal at the server side. Perhaps there...
1
3194
by: Harry Keck | last post by:
Is there a set of methods in the .Net framework that do the same thing as the jscript functions escape and unescape? I get very close with HttpUtility.UrlEncodeUnicode, but it puts "+" for spaces instead of "%20". Uri.EscapeString seems to create the correct escape string, but there is no unescape. The function is not public anyway, so I probably shouldn't use it. I need a way to escape text on the client, and then unescape it on the...
2
28233
by: Richard Lewis Haggard | last post by:
Is it possible to use features from XmlDocument to unescape text back to its original raw text format after it has been escaped to handle non-HTML compliant character strings? I have code that serializes text to an XML file and then deserializes back to text. If the user enters XML illegal text like "<Actor1>", the code properly escapes it to "&lt;Actor1&gt;", something that doesn't interfere with the XML syntax, and writes it into the Xml...
0
1530
by: gomzi | last post by:
hi, I would like to know as to what the equivalent functions are in .net for the escape and unescape functions that we have in javascript. tried both Server.HtmlEncode & Server.UrlEncode without much success. i.e. when i have something like this... string-> how'zat'
1
4186
by: RK | last post by:
Perhaps I'm confused somwhere in my understanding, but I'm trying to deal with some escaped text from an XML file which includes html markup and it appears there are a couple of techniques for escape codes. (I hope I can describe my issue here without having to worry about how to escape it for display on this group!) On the server-side I extract some text from an XML file, which includes some style and content. The XML is simply:
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9184
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6737
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6033
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4551
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.