Solved

Do a regex to limit the find and replace

Posted on 2012-04-10
4
255 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:gurvinder372
Comment Utility
yes you can

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

Accepted Solution

by:
Proculopsis earned 425 total points
Comment Utility
<!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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now