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

3
  • 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. Commented May 4, 2011 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? Commented May 4, 2011 at 14:48
  • (We can make a reasonably good guess that the conversion to HTML is not being done by JavaScript) Commented May 4, 2011 at 14:50

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for a useful library that will help with my few PHP projects.
0

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

newstring = yourstring.Replace("&", "&");

2 Comments

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
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
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

1 Comment

Thanks, but I'm looking for a way to do this using JavaScript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.