?
Solved

Do a regex to limit the find and replace

Posted on 2012-04-10
4
Medium Priority
?
283 Views
Last Modified: 2012-04-10
Hi

I need to do a find and replace ("/" with " / ") with java script - but only within <div> tag of a certain class.

So this:
Title 1/2 <div class="theclass">Option1/Option2/Option3</div>
Title 3/4 <div class="theclass">OptionA/OptionB/OptionC</div>

Would get changed to this:
Title 1/2 <div class="theclass">Option1 / Option2 / Option3</div>
Title 3/4 <div class="theclass">OptionA / OptionB / OptionC</div>

Can I use a javascript regex to do this?

Thanks,
Matt
0
Comment
Question by:gameon_238
4 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 37827244
yes you can

$(".theclass").each(function(){
  var html = $(this).html();
  html  = html.replace("/", " / ");
});
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 1275 total points
ID: 37827263
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>
 
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
 
<script type='text/javascript'>//<![CDATA[
$(function(){

$(".theclass").each( function() {

    var text = $(this).text();
    text = text.replace( /\//g, " / " );
    $(this).text( text );

});
});//]]>  

</script>

</head>
<body>
  Title 1/2 <div class="theclass">Option1/Option2/Option3</div>
Title 3/4 <div class="theclass">OptionA/OptionB/OptionC</div>

</body>
</html>
0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 37830128
Modern browsers do have the querySelectorAll method.
Remember that the method returns a nodelist so you have to loop through it.
var allElements = document.querySelectorAll(".theclass");
for(var x = 0, len = allElements.length; x < len; x++) {
   var el = allElements.item(x), 
       prop = (el.textContent) ? "textContent" : "innerText";
   el[prop] = el[prop].replace(/\//g, " / ");
}

Open in new window

0
 

Author Comment

by:gameon_238
ID: 37830834
Thank you all for the suggestions!

Proculopsis:
Please can you comment the code?
- why does $(function(){ cuase the function to run straight away?

Why can I use something like this to choose when to run the function:

<script type='text/javascript'>
      function dorep(){
      
            $(".theclass").each( function() {
            
                var text = $(this).text();
                text = text.replace( /\//g, " / " );
                $(this).text( text );
            
            });
            
      }
      dorep();
</script>

Is it becuase it would need to be called after the page had loaded?

Can I limit this futher to only apply to a string of text passes to it (containing divs with those classes - rather than all the html content ...)

Thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

839 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