By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,921 Members | 1,959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,921 IT Pros & Developers. It's quick & easy.

turn quotes into " and apostrophes into '

P: n/a

Hi,

I'm looking for a javascript function that will convert input such as this:

<CLUB Code="

into this:

&lt;CLUB Code=&quot;
First, I thought the "escape" function would do it, but that does
something different.

The reason I want such a function is that we're using msxml's
xmlhttprequest to send xml , and for some reason the stuff inside the
soap envelope neds to be quoted as above.

Do I need to write this function myself (I'll probably use "regular
expressions" if I write it myself) or is there an existing function I
can call ?

Thanks. er*******@rcn.com 4/12/04

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Eric Osman wrote:
I'm looking for a javascript function that will convert input such as
this:

<CLUB Code="

into this:

&lt;CLUB Code=&quot;

<snip>

An example of such a function can be found at:-

<URL: http://www.crockford.com/javascript/remedial.html >

Richard.
Jul 23 '05 #2

P: n/a


Richard Cornford wrote:
Eric Osman wrote:
I'm looking for a javascript function that will convert input such as
this:

<CLUB Code="

into this:

&lt;CLUB Code=&quot;


<snip>

An example of such a function can be found at:-

<URL: http://www.crockford.com/javascript/remedial.html >

Richard.

THanks Richard,

The routines I wrote look like this:

//+-------------------------------------------------------------
//| quoteXml puts appropriate quoting around various elements so
//| that was interpretable xml (or html) code will now be
//| displayable.
//| A new string is returned.
//+-------------------------------------------------------------
function quoteXml (xmlStr) {
var result = new EditableString(xmlStr);
result = result.replaceAll("<", "&lt;");
result = result.replaceAll(">", "&gt;");
result = result.replaceAll("\"", "&quot;");
result = result.replaceAll("'", "&apos;");
return result.data;
}

//+-------------------------------------------------------------
//| unquoteXml removes the quote marks, which is useful when
//| the returned stuff wants to be parsed as xml.
//+-------------------------------------------------------------
function unquoteXml (xmlStr) {
var result = new EditableString(xmlStr);
result = result.replaceAll("&lt;", "<");
result = result.replaceAll("&gt;", ">");
result = result.replaceAll("&quot;", "\"");
result = result.replaceAll("&apos;", "'");
return result.data;
}

Of course, to use them, you need my EditableString object definition too:

//+-------------------------------------------------------------
//| Object type editableString is a string that can be edited with
//| a number of useful methods contained below.
//+-------------------------------------------------------------
function EditableString(str) {
this.data = str;
}

//+-------------------------------------------------------------
//| replaceAll replaces all source strings with destination strings,
//| returning a new EditableString containing the result.
//+-------------------------------------------------------------
EditableString.prototype.replaceAll = function (srcStr, dstStr) {
this.pat = new RegExp(srcStr,"g");
var newStr = this.data.replace (this.pat, dstStr);
return new EditableString(newStr);
}
p.s. I often use an html "<textarea>" in which to display html that I
want to display for the person to see, so they angle brackets and
tags can all be seen.

However, I discovered that this is fine until you try to display
something like "&guot;" in a textarea. When you attempt that,
the browser (well, ie6 anyway) changes it back into a quote mark!

So, in order to make sure "&quot;" gets properly displayed, I
ended up having to change the ampersand to "&amp;" , so for
example, if var requestBody has some stuff in it that I wasnt to
display in a textarea, and that stuff might have "&quot;" in it,
I further quote it like this:

new EditableString(requestBody).replaceAll("&","&amp;" ).data)

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.