473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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&lt;/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 7607
Philipp wrote on 20 dec 2007 in comp.lang.javascript:
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&lt;/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
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...
6
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...
3
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
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: ...
2
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. ...
1
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...
2
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...
0
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...
1
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.