Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > turn quotes into " and apostrophes into '

Reply
Thread Tools

turn quotes into " and apostrophes into '

 
 
Eric Osman
Guest
Posts: n/a
 
      04-12-2004

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. http://www.velocityreviews.com/forums/(E-Mail Removed) 4/12/04

 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      04-14-2004
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.


 
Reply With Quote
 
 
 
 
Eric Osman
Guest
Posts: n/a
 
      04-14-2004


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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Escaping apostrophes inserting into sql mister-Ed ASP .Net 1 10-05-2007 07:09 PM
Quotes or apostrophes showing up as code Richard Fangnail Computer Support 4 08-03-2007 06:05 PM
Quotes/Double Quotes in Image Control Chris White ASP .Net 1 09-22-2004 06:22 AM
inserting apostrophes into DB? Lord Merlin ASP General 4 07-02-2004 03:50 PM
XMLHTTP: translating entities like curly quotes and curly apostrophes? Ken Fine ASP General 2 02-24-2004 11:17 AM



Advertisments