Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove "\" from strings in javascript

I'm having some trouble removing slashes in Javascript.

I've got some HTML coming through via JSON and need to get rid of the escaping slashes.

Here's what I have now, but it doesn't seem to work - at all...

obj.embed_code = obj.embed_code.replace(/\\/g, '');

The HTML inside that object is an embed iframe from YouTube, but since it's got the escaped quotes I just end up with an iframe that's got a 404 page in it.

What would be the correct way to do this?

Here's an example of what the output looks like...

<iframe width=\\\"420\\\" height=\\\"315\\\" src=\\\"http:\/\/www.youtube.com\/embed\/MD61itbPNEY\\\" frameborder=\\\"0\\\" allowfullscreen><\/iframe>

In addition, here's the page where the JSON is being pulled from so you can see what it looks like raw...

http://bit.ly/UxuvfL

like image 998
user1146223 Avatar asked Jan 21 '26 04:01

user1146223


1 Answers

If the data is truly JSON then the correct way would be to use a JSON parser, as there's more to JSON parsing than simply removing backslashes. Given something like

{"content":"<p>CleverStuff\u2122<\/p>"}

JSON.parse(theData).content would give you

<p>CleverStuff™</p>
like image 93
Ian Roberts Avatar answered Jan 23 '26 20:01

Ian Roberts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!