Solved

Do a regex to limit the find and replace

Posted on 2012-04-10
4
266 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

Industry Leaders: 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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

688 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