?
Solved

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

Posted on 2008-02-04
3
Medium Priority
?
710 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:InsertCleverName
  • 2
3 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20815712
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

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 400 total points
ID: 20815744
Here without the hidden div

<div id="x">
&bull; Bullet<br>
&bull; Bullet<br>
&bull; Bullet<br>
&bull; Bullet<br>
</div>
<script>
var s = document.getElementById('x').innerHTML;
var re = eval('/'+unescape('%u2022')+'/g')
var s=s.replace(re,'&copy;');
document.getElementById('x').innerHTML=s;
</script>
0
 

Author Closing Comment

by:InsertCleverName
ID: 31427809
Nice!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question