reg exp to clean html tags

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ma.giorgi@gmail.com

    reg exp to clean html tags

    hi to all!
    I've tried in all the way but I can't find a solution
    I show you an example:
    I have the following html code:

    <div id="aaa">text inside<br/> a div</div><span class="bbb"> text
    inside a span</span><img src="blabla"/><p class='ccc'
    style='blablabl a'>text inside a <b style="FONT-SIZE:
    14px">paragraph </b> with some<!--Hey I'm a f$*!#ng comment--> bold
    text</p>

    I need to tranform this text into clean html, like this:

    text inside a div text inside a span<p>text inside a paragraph with
    some bold text</p>

    I've tried this:

    function cleareTags(){
    var stringToReplace = String(document .theform.Articl eText.value);
    var re=/(<\/?p)(?:\s[^>]*)?(>)|<[^>]*>/gi;
    document.thefor m.ArticleText.v alue = stringToReplace .replace(re,'') ;
    }

    but nothing...it removes all the tags

    I've tried also this way

    function cleareTags(){
    var r=/<(\w{1})([^>]*)>(.*)<\/\1>/gmi;
    document.thefor m.ArticleText.v alue = stringToReplace .replace(r,
    function(s,tag, attr,inner) {
    //alert (inner)
    return
    (tag.toUpperCas e()=="P"?"<"+ta g+">"+inner+" </"+tag+">":inner );
    }
    );
    }

    but nothing happen...

    please help me
    My resources are at the end
    bye
    marco

  • Jc

    #2
    Re: reg exp to clean html tags

    ma.giorgi@gmail .com wrote:[color=blue]
    > hi to all!
    > I've tried in all the way but I can't find a solution
    > I show you an example:
    > I have the following html code:
    >
    > <div id="aaa">text inside<br/> a div</div><span class="bbb"> text
    > inside a span</span><img src="blabla"/><p class='ccc'
    > style='blablabl a'>text inside a <b style="FONT-SIZE:
    > 14px">paragraph </b> with some<!--Hey I'm a f$*!#ng comment--> bold
    > text</p>
    >
    > I need to tranform this text into clean html, like this:
    >
    > text inside a div text inside a span<p>text inside a paragraph with
    > some bold text</p>[/color]

    You can probably do this using regular expression's and a replace()
    call, using the buffer and non-greedy operators (as one option), but
    rather than write such a regex for you it would be best if you could
    allow an actual HTML parser (such as a browser) to do the work for you.

    If you don't care about browser compatibility (you are using this just
    for yourself), you could use innerHTML and innerText in IE. For
    example, add the HTML to an object in the document using innerHTML or
    insertAdjacentH TML, and then read it back using innerText.

    Comment

    • ma.giorgi@gmail.com

      #3
      Re: reg exp to clean html tags

      thanks for the answer...
      the compatibility is to be only with IE6

      but sorry I haven't understood quite well your answer...javasc ript it's
      not my language and the boss has given me anyway this problem to solve

      how can I do with innerHTML?

      Comment

      • Jc

        #4
        Re: reg exp to clean html tags

        ma.giorgi@gmail .com wrote:[color=blue]
        > thanks for the answer...
        > the compatibility is to be only with IE6
        >
        > but sorry I haven't understood quite well your answer...javasc ript it's
        > not my language and the boss has given me anyway this problem to solve
        >
        > how can I do with innerHTML?[/color]

        Here's an example:

        <body>

        <div id="divContaine r"></div>

        <script>
        var sHTML = "<div>a<div >b</div></div>";
        divContainer.in nerHTML = sHTML;
        alert(divContai ner.innerText);
        </script>

        </body>

        Comment

        • Stephen Chalmers

          #5
          Re: reg exp to clean html tags

          <ma.giorgi@gmai l.com> wrote in message news:1119028881 .790811.137600@ g43g2000cwa.goo glegroups.com.. .[color=blue]
          > hi to all!
          > I've tried in all the way but I can't find a solution
          > I show you an example:
          > I have the following html code:
          >
          > <div id="aaa">text inside<br/> a div</div><span class="bbb"> text
          > inside a span</span><img src="blabla"/><p class='ccc'
          > style='blablabl a'>text inside a <b style="FONT-SIZE:
          > 14px">paragraph </b> with some<!--Hey I'm a f$*!#ng comment--> bold
          > text</p>
          >
          > I need to tranform this text into clean html, like this:
          >
          > text inside a div text inside a span<p>text inside a paragraph with
          > some bold text</p>
          >
          > I've tried this:
          >
          > function cleareTags(){
          > var stringToReplace = String(document .theform.Articl eText.value);
          > var re=/(<\/?p)(?:\s[^>]*)?(>)|<[^>]*>/gi;
          > document.thefor m.ArticleText.v alue = stringToReplace .replace(re,'') ;
          > }
          >
          > but nothing...it removes all the tags
          >
          > I've tried also this way
          >
          > function cleareTags(){
          > var r=/<(\w{1})([^>]*)>(.*)<\/\1>/gmi;
          > document.thefor m.ArticleText.v alue = stringToReplace .replace(r,
          > function(s,tag, attr,inner) {
          > file://alert (inner)
          > return
          > (tag.toUpperCas e()=="P"?"<"+ta g+">"+inner+" </"+tag+">":inner );
          > }
          > );
          > }
          >
          > but nothing happen...
          >
          > please help me
          > My resources are at the end
          > bye
          > marco
          >[/color]

          I assume you're trying to remove all tags except <p></p>
          Unless someone knows another way, you can do it in two separate operations.
          The first removes all tags except <p></p> but does not remove parameters within the opening <p> tag.

          str=str.replace (/<(?!\s*p\s*|\w> |\s*\/\s*p\s*>)[^>]+>/gi, "")

          str=str.replace (/<\s*p[^>]+>/gi, "<p>");

          This isn't a universal solution, but on recent browsers it works on your example text.
          --
          Stephen Chalmers http://makeashorterlink.com/?H3E82245A

          547265617375726 520627572696564 206174204F2E532 E207265663A2054 51323437393134



          Comment

          • Stephen Chalmers

            #6
            Re: reg exp to clean html tags

            <ma.giorgi@gmai l.com> wrote in message news:1119028881 .790811.137600@ g43g2000cwa.goo glegroups.com.. .[color=blue]
            > hi to all!
            > I've tried in all the way but I can't find a solution
            > I show you an example:
            > I have the following html code:
            >
            > <div id="aaa">text inside<br/> a div</div><span class="bbb"> text
            > inside a span</span><img src="blabla"/><p class='ccc'
            > style='blablabl a'>text inside a <b style="FONT-SIZE:
            > 14px">paragraph </b> with some<!--Hey I'm a f$*!#ng comment--> bold
            > text</p>
            >
            > I need to tranform this text into clean html, like this:
            >
            > text inside a div text inside a span<p>text inside a paragraph with
            > some bold text</p>
            >
            > I've tried this:
            >
            > function cleareTags(){
            > var stringToReplace = String(document .theform.Articl eText.value);
            > var re=/(<\/?p)(?:\s[^>]*)?(>)|<[^>]*>/gi;
            > document.thefor m.ArticleText.v alue = stringToReplace .replace(re,'') ;
            > }
            >
            > but nothing...it removes all the tags
            >
            > I've tried also this way
            >
            > function cleareTags(){
            > var r=/<(\w{1})([^>]*)>(.*)<\/\1>/gmi;
            > document.thefor m.ArticleText.v alue = stringToReplace .replace(r,
            > function(s,tag, attr,inner) {
            > file://alert (inner)
            > return
            > (tag.toUpperCas e()=="P"?"<"+ta g+">"+inner+" </"+tag+">":inner );
            > }
            > );
            > }
            >
            > but nothing happen...
            >
            > please help me
            > My resources are at the end
            > bye
            > marco
            >[/color]
            The first operation removes all tags except <p></p>; the second removes parameters within the opening <p> tag.
            It won't handle nested <> characters.
            I have avoided the use of lookahead assertions.

            str=str.replace (/<\s*([a-oq-z]|p\w|\!)[^>]*>|<\s*\/\s*([a-oq-z]|p\w)[^>]*>/gi, "");

            str=str.replace (/<\s*p[^>]+>/gi, "<p>");

            --
            Stephen Chalmers








            Comment

            Working...