Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    41
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Find text pattern/string and remove it from HTML source?

    Assume I have a HTML code similar to

    ...
    <ul>
    <li>...</li>
    <li>...</li>
    ....
    aaa
    <br>
    bbb
    <br>
    ccc
    blahblah
    </ul>
    ...

    Now I want to search from JavaScript for a pattern

    <br>bbb<br>ccc

    and delete it form HTML source. Be aware that I don't want to delete "aaa" or "blahblah" or even the whole <ul>...</ul> element.

    So I can NOT use e.g. a JS code like:

    $("ul:contains('<br>bbb<br>ccc')").css("display", "none");

    Is there another way to delete a certain text (pattern) from HTML source (maybe with the help of jQuery)?

    Thank you
    Peter

  • #2
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    You can work with document.body.innerHTML to get or set the body content.
    Code:
    var content=document.body.innerHTML;
    alert(content)
    newContent=content.replace(/[\n\r\t]*<br>[\n\r\t]*bbb[\n\r\t]*<br>[\n\r\t]*ccc/g,'');
    alert(newContent);
    document.body.innerHTML=newContent;
    Then the problem is to replace any carriage return, linefeed or tabulations with a regular expression ([\n\r\t]* for one or more of this invisible characters).
    Last edited by 007julien; 02-17-2014 at 10:33 AM.

  • Users who have thanked 007julien for this post:

    pstein (02-17-2014)


  •  

    Posting Permissions

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