0

I have an array of chars that I would like to do a find and replace with jquery . The array is:

var f = ['“','â€','‘'','’','…','—','–'];

var r = ['"','"',"'","'","...","-","-"];

I have tried a few different ideas, but nothing is working. I setup a fiddle to work on it here:

http://jsfiddle.net/npGRF/1/

Any suggestions?

3 Answers 3

3

In this particular sample the problem isn't your replace function but your jquery selector. If you replace $(this) with $('body') it works. That said, I'm not sure copying your entire page's HTML into a string, doing replacing on it, and then putting that string back into your body is the most efficient thing. Maybe you can narrow it down to smaller parts, like the p tags in your content, which you could perhaps label with a class to eliminate other p tags from the replace.

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

Comments

2

You're doing it right. :)

In your fiddle the selector $(this) was wrong, so there was no text. You can use the id of your element or the whole body instead. Additionally you forgot to reset the text to dom. The replacing itself is working fine.

var text = $('#wfmrdablogspot').html();
var f = ['“', 'â€', '‘', '’', '…', '—', '–'];
var r = ['"', '"', "'", "'", "...", "-", "-"];
text = text.replaceArray(f, r);
$('#wfmrdablogspot').html(text);

http://jsfiddle.net/npGRF/2/

Comments

0

Some basic debugging shows that $(this).html() does not work (this is resolved as the current Window). Use $(document.body) instead.

var html = $(document.body).html();
var f = ['“', 'â€', '‘', '’', '…', '—', '–'];
var r = ['"', '"', "'", "'", "...", "-", "-"];
var newHtml = html.replaceArray(f, r);

$(document.body).html(newHtml);

See http://jsfiddle.net/npGRF/3/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.