Link to home
Start Free TrialLog in
Avatar of InsertCleverName
InsertCleverName

asked on

How to match special HTML characters using reg ex in JavaScript?

I'm trying to use a regular expression on a snippet of HTML that contains • (ampersand then 'bull;').

I'm getting the HTML by doing:

var s=document.body.innerHTML;

but this returns the HTML as parsed into a DOM and the special characters - prefixed with ampersands - don't seem to be prefixed with ampersands anymore once the DOM has been created.  So just doing:

var arr=s.match(/•/);

won't return anything.
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Hmm, that was interesting
<div id="x">
&bull; Bullet<br>
&bull; Bullet<br>
&bull; Bullet<br>
&bull; Bullet<br>
</div>
<div id="y" style="display:none">&bull;</div>
<script>
var s = document.getElementById('x').innerHTML;
var bull = document.getElementById('y').innerHTML;
var re = eval('/'+bull+'/g')
var s=s.replace(re,'&copy;');
document.getElementById('x').innerHTML=s;
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of InsertCleverName
InsertCleverName

ASKER

Nice!