Solved

Do a regex to limit the find and replace

Posted on 2012-04-10
4
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37827244
yes you can

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

Accepted Solution

by:
Proculopsis earned 425 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
output in HTML in Powershell 5 55
Problem in javascript 17 51
allow link tag to be submit like form 15 53
Checkbox changes display when resizing window 8 22
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Find out what you should include to make the best professional email signature for your organization.
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 …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

738 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