www.webdeveloper.com
Results 1 to 3 of 3

Thread: Remove text using JS

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Posts
    34

    Remove text using JS

    Consider I ahve the following text:

    <h4>The quick brown fox jumps over the lazy dog and feels as if he were in the seventh heaven. Why does it jump quick over a dog?</h4>

    Now is it possible to use Javascript to scan this peice of text and remove certain words?

    If I want to remove "and feels" as well as "quick"

    I should end up with this:

    <h4>The brown fox jumps over the lazy dog as if he were in the seventh heaven. Why does it jump over a dog?</h4>

    Is this possible? If so can someone provide a working example.

    Many thanks

  2. #2
    Join Date
    Apr 2006
    Location
    Perth
    Posts
    154
    Yes, it is very possible. You need to learn something about Regular Expressions.
    aka RegEx and RegExp. There are thousands of tutorials when you search google.

    Code:
    <html>
     <script language="javascript">
    
      inpStr="The quick brown fox jumps over the lazy dog and feels as if he were in the seventh heaven. Why does it jump quick over a dog?"
    
      rAry=new Array("and feels","quick");
    
      for(i=0;i<rAry.length;i++)
       inpStr=inpStr.replace(RegExp(rAry[i],"g"),"");
    
      document.write(inpStr);
    
     </script>
    </html>
    Colin Fiat Simple Solutions To Complex Problems

  3. #3
    Join Date
    Aug 2010
    Posts
    34
    Thank you for your reply.

    And yes, Google is my friend. I tried your code, and it works. However it only works on the text you declare in the var and spits it out without whatever has been specified. What If I wanted the script to scan a page which all ready has the text on it. I.E scan the page and delete/replace certain words.

    Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles