regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bortolaso@softhome.net

    #1

    regular expression

    hi all, i already write a question on regular expression but i do a
    mistake in problem definition.
    I'm loking for a regular expression that delete the \r\n from HTML TAG
    like the follow example:

    from:
    <P
    class=try> try
    try </P>

    to:

    <P class=prova> try
    try </P>

    Note: the \r\n symmbols must be deleted from html tag and not form
    text.


    thank. Simone

  • BMR

    #2
    Re: regular expression

    Try :
    texte.replace(/(<.*)\r?\n?(.*> )/g, "$1$2");

    BMR

    bortolaso@softh ome.net a écrit :[color=blue]
    > hi all, i already write a question on regular expression but i do a
    > mistake in problem definition.
    > I'm loking for a regular expression that delete the \r\n from HTML TAG
    > like the follow example:
    >
    > from:
    > <P
    > class=try> try
    > try </P>
    >
    > to:
    >
    > <P class=prova> try
    > try </P>
    >
    > Note: the \r\n symmbols must be deleted from html tag and not form
    > text.
    >
    >
    > thank. Simone
    >[/color]

    Comment

    • John Doe

      #3
      Re: regular expression

      The previous reply is not even close to a solution (it won't even remove
      a single \r\n, much less any of the other possible permutations of the
      problem). The following is probably inelegant. I imagine there's
      someone out there who could write it with a single regex, but the
      following does actually work at least:
      <code>
      <head>
      <title>
      Regex + recursive function = solution
      </title>
      <script>
      var mlink=new String("<p \r\r\n\n \n \r \r\n \r\r\r\n \r\n\n\n \r\r\r
      \n\n\n id=\r\nmt\r\n \r\n >P\r\n Text</\r\np><b \r\n i\r\nd\r\n=\nbo ld
      \r\n>bold<\r\n/b>");
      function cleanlink(mystr ) {
      alert("Original :"+mystr);
      var myrg1=/(<[^>]*?)\r*\n+([^>]*?>)/g;
      var myrg2=/(<[^>]*?)\r+\n*([^>]*?>)/g;
      var myrgchk1=/(<[^>]*?)\r+([^>]*?>)/;
      var myrgchk2=/(<[^>]*?)\n+([^>]*?>)/;
      var rcrun = function(myst) {
      myst=myst.repla ce(myrg1,"$1$2" );
      myst=myst.repla ce(myrg2,"$1$2" );
      if (myst.search(my rgchk1) != -1 || myst.search(myr gchk2) != -1 ) {
      alert("Recursin g");
      myst=rcrun(myst );
      }
      return myst;
      };
      mystr=rcrun(mys tr);
      alert("Now:"+my str);
      return mystr;
      }
      mlink=cleanlink (mlink);
      alert("Final:"+ mlink);
      </script>
      </head>
      <body>
      Hello World!
      </body>
      </html>


      In article <41f8b124$0$166 06$636a15ce@new s.free.fr>, bmr@ediweb.org
      says...[color=blue]
      > Try :
      > texte.replace(/(<.*)\r?\n?(.*> )/g, "$1$2");
      >
      > BMR
      >
      > bortolaso@softh ome.net a écrit :[color=green]
      > > hi all, i already write a question on regular expression but i do a
      > > mistake in problem definition.
      > > I'm loking for a regular expression that delete the \r\n from HTML TAG
      > > like the follow example:
      > >
      > > from:
      > > <P
      > > class=try> try
      > > try </P>
      > >
      > > to:
      > >
      > > <P class=prova> try
      > > try </P>
      > >
      > > Note: the \r\n symmbols must be deleted from html tag and not form
      > > text.
      > >
      > >
      > > thank. Simone
      > > [/color]
      > [/color]

      Comment

      • John Doe

        #4
        Re: regular expression

        You will of course want to remove all the alerts in a production version
        In article <1106821450.d22 67f7ca747a61a5c 2499524ceefdc0@ meganetnews2>,
        spaim@L!*THIS says...[color=blue]
        > alert("Original :"+mystr);
        > alert("Recursin g");
        > alert("Now:"+my str);
        > alert("Final:"+ mlink);[/color]

        Comment

        • BMR

          #5
          Re: regular expression

          > The previous reply is not even close to a solution (it won't even
          remove a single \r\n, much less any of the other possible permutations
          of the problem).

          I'm not an english-speaking person, but the problem here should not be
          "it won't" but "it doesn't". BTW did you try it ?

          BMR

          John Doe a écrit :
          The following is probably inelegant. I imagine there's[color=blue]
          > someone out there who could write it with a single regex, but the
          > following does actually work at least:
          > <code>
          > <head>
          > <title>
          > Regex + recursive function = solution
          > </title>
          > <script>
          > var mlink=new String("<p \r\r\n\n \n \r \r\n \r\r\r\n \r\n\n\n \r\r\r
          > \n\n\n id=\r\nmt\r\n \r\n >P\r\n Text</\r\np><b \r\n i\r\nd\r\n=\nbo ld
          > \r\n>bold<\r\n/b>");
          > function cleanlink(mystr ) {
          > alert("Original :"+mystr);
          > var myrg1=/(<[^>]*?)\r*\n+([^>]*?>)/g;
          > var myrg2=/(<[^>]*?)\r+\n*([^>]*?>)/g;
          > var myrgchk1=/(<[^>]*?)\r+([^>]*?>)/;
          > var myrgchk2=/(<[^>]*?)\n+([^>]*?>)/;
          > var rcrun = function(myst) {
          > myst=myst.repla ce(myrg1,"$1$2" );
          > myst=myst.repla ce(myrg2,"$1$2" );
          > if (myst.search(my rgchk1) != -1 || myst.search(myr gchk2) != -1 ) {
          > alert("Recursin g");
          > myst=rcrun(myst );
          > }
          > return myst;
          > };
          > mystr=rcrun(mys tr);
          > alert("Now:"+my str);
          > return mystr;
          > }
          > mlink=cleanlink (mlink);
          > alert("Final:"+ mlink);
          > </script>
          > </head>
          > <body>
          > Hello World!
          > </body>
          > </html>
          >
          >
          > In article <41f8b124$0$166 06$636a15ce@new s.free.fr>, bmr@ediweb.org
          > says...
          >[color=green]
          >>Try :
          >>texte.replace (/(<.*)\r?\n?(.*> )/g, "$1$2");
          >>
          >>BMR
          >>
          >>bortolaso@sof thome.net a écrit :
          >>[color=darkred]
          >>>hi all, i already write a question on regular expression but i do a
          >>>mistake in problem definition.
          >>>I'm loking for a regular expression that delete the \r\n from HTML TAG
          >>>like the follow example:
          >>>
          >>>from:
          >>><P
          >>>class=try> try
          >>>try </P>
          >>>
          >>>to:
          >>>
          >>><P class=prova> try
          >>>try </P>
          >>>
          >>>Note: the \r\n symmbols must be deleted from html tag and not form
          >>>text.
          >>>
          >>>
          >>>thank. Simone
          >>>[/color]
          >>[/color][/color]

          Comment

          • Fred Oz

            #6
            Re: regular expression

            BMR wrote:[color=blue][color=green]
            > > The previous reply is not even close to a solution (it won't even[/color]
            > remove a single \r\n, much less any of the other possible permutations
            > of the problem).
            >
            > I'm not an english-speaking person, but the problem here should not be
            > "it won't" but "it doesn't". BTW did you try it ?[/color]

            It could be either - it doesn't for me and it won't if I try
            again.

            Here is a solution that parses the string, refusing to put \r or
            \n into the new array if found inside a tag '< >' pair:


            <html><head><ti tle>Parse solution</title>

            <script type="text/javascript">

            var str = '<p \r\r\n\n \n \r \r\n '
            + '\r\r\r\n \r\n\n\n \r\r\r'
            + '\n\n\n id=\r\n"mt"\r\n \r\n >P\r\n '
            + 'Text</\r\np><b \r\n i\r\nd\r\n=\n"b old" '
            + '\r\n>bold<\r\n/b>'
            ;

            var s = str.split('');
            var t = [];
            var inTag = false;

            for (var i=0, len=s.length; i<len; i++) {
            if (s[i] == '<') inTag = true;
            if (s[i] == '>') inTag = false;

            if (inTag && escape(s[i]) != '%0D' && escape(s[i]) != '%0A') {
            t.push(s[i]);
            } else if (!inTag) {
            t.push(s[i]);
            }
            }

            alert('t is: \n\n' + t.join(''));

            </script>
            </head>
            <body>
            empty document

            </body>
            </html>




            --
            Fred

            Comment

            • John Doe

              #7
              Re: regular expression

              [color=blue][color=green]
              > > I'm not an english-speaking person, but the problem here should not be
              > > "it won't" but "it doesn't". BTW did you try it ?[/color]
              >
              > It could be either - it doesn't for me and it won't if I try
              > again.[/color]
              Exactly. It depends on intent. Since my reply was to the original
              poster of the question I was informing them that it won't work for them
              when and or if they should try it. Grammar accusations are a poor cover
              for shoddy programming. (Of course my tact, admitting to the shoddy
              programming ahead of time, isn't much better). The REAL question is
              "Did YOU try it?" because if you had you would know that it is
              worthless.[color=blue]
              >
              > Here is a solution that parses the string, refusing to put \r or
              > \n into the new array if found inside a tag '< >' pair:
              >[/color]
              Good alternative! I'm not sure if it's more or less elegant than the
              recursive version, but I'm guessing with javascript's poor regex
              performance this's probably better.[color=blue]
              >
              > <html><head><ti tle>Parse solution</title>
              >
              > <script type="text/javascript">
              >
              > var str = '<p \r\r\n\n \n \r \r\n '
              > + '\r\r\r\n \r\n\n\n \r\r\r'
              > + '\n\n\n id=\r\n"mt"\r\n \r\n >P\r\n '
              > + 'Text</\r\np><b \r\n i\r\nd\r\n=\n"b old" '
              > + '\r\n>bold<\r\n/b>'
              > ;
              >
              > var s = str.split('');
              > var t = [];
              > var inTag = false;
              >
              > for (var i=0, len=s.length; i<len; i++) {
              > if (s[i] == '<') inTag = true;
              > if (s[i] == '>') inTag = false;
              >
              > if (inTag && escape(s[i]) != '%0D' && escape(s[i]) != '%0A') {
              > t.push(s[i]);
              > } else if (!inTag) {
              > t.push(s[i]);
              > }
              > }
              >
              > alert('t is: \n\n' + t.join(''));
              >
              > </script>
              > </head>
              > <body>
              > empty document
              >
              > </body>
              > </html>[/color]

              Comment

              Working...