0

Does anyone know if there exists a built in JavaScript function to turn & into &

so it would turn the string something1 & something2 into something1 & something2

of course I can use MyString.replace(/\&/g, "&"); but I would rather not.

I tried using the following with no joy: decodeURIComponent unescape

Thanks

Bob
  • 4,236
  • 12
  • 45
  • 65
  • You haven't told us where this string starts out (typed in by a user? read from a database? hard coded into a JS file? hard coded in to an ASPX file? etc?), what happens to it, or where you are reading it back from, so this question is pretty much unanswerable. All we can say for certain is that at some point the string is being expressed as (X)HTML. – Quentin May 04 '11 at 14:46
  • Also, although you say you want it in its original format, you don't say why. Do you just want a text representation of the string (as opposed to an HTML one)? Do you really want to know exactly what it was before it was encoded? Something else? – Quentin May 04 '11 at 14:48
  • (We can make a reasonably good guess that the conversion to HTML is not being done by JavaScript) – Quentin May 04 '11 at 14:50

3 Answers3

1

You might want to look at the phpJS project, which has produced Javascript implementations of most of PHP's built-in functions.

This includes JS implementations of htmlentities() and html_entity_decode().

In case you find the phpJS code poorly written (it is), you might want to try this instead: http://css-tricks.com/snippets/javascript/htmlentities-for-javascript/

This only provided one function to encode the string with entities (you're looking for decoding, which is why I originally linked you to the phpJS functions), but the decode function should be fairly easy to work out from this example.

Spudley
  • 166,037
  • 39
  • 233
  • 307
0

It'd be helpful to know where you are encountering this issue. It's more likely that this is happening server side, in which case you can decode it using System.Web.HttpUtility.HtmlDecode

Tim Ebenezer
  • 2,714
  • 17
  • 23
0

Can't you just do the following in JavaScript or C#?

newstring = yourstring.Replace("&", "&");
WEFX
  • 8,298
  • 8
  • 66
  • 102
  • yes I can do that. But i would like to use a built in function to decode as there may be other decoding that may need to happen. Don't want to hard-code it for ampersands – Bob May 04 '11 at 15:44
  • Here is an answer I wrote on scanning-for and removing HTML tags, etc, using Regex. You might be able to do something similar, but search for any Regex string that matches "&.+;" (where "." is any character, and "+" means to check for one-or-more "."). [link](http://stackoverflow.com/questions/286813/how-do-you-convert-html-to-plain-text/5276721#5276721) – WEFX May 04 '11 at 16:51