467,864 Members | 1,732 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,864 developers. It's quick & easy.

turn quotes into " and apostrophes into '


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
  • viewed: 19546
Share:
2 Replies
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


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.

Similar topics

1 post views Thread by C. Titus Brown | last post: by
2 posts views Thread by Joe Gass | last post: by
4 posts views Thread by barney | last post: by
2 posts views Thread by Johnny Ljunggren | last post: by
3 posts views Thread by katieh | last post: by
3 posts views Thread by LionelAndJen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.