Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Do a regex to limit the find and replace

Posted on 2012-04-10
4
Medium Priority
?
281 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: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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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 …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

618 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