Hi,

I'm trying to get an option to remove BBcode in a textarea like what they have here (the button with the big red X when you post) e.g.

[/ quote] [font= ][/font ] etc.

I've managed to read the contents of the textarea into a function and I've tried a few regex's but nothing seems to work.

Most of the regexs I've tried are to get the contents of the [] tags but I want to remove the content and the [] tags themselves.

Any ideas???

    The regex would be something like:

    /\[[^\]]*\]/
    

    (Is that the ugliest thing you've ever seen in so few characters, or what? 😉 )

      That's damn fugly 😉

      I tried it without success.

      The bbcode I tried it on was

      [quote ]hello[/quote ]
      

      The Javascript function is:

      function removeBBcode(areaId)
      {
      var txtArea = document.getElementById( areaId );
      pattern = new RegExp ("/\[[^\]]*\]/");
      document.getElementById(areaId).value = document.getElementById(areaId ).value.replace(pattern,"");
      
      if (document.getElementById( areaId ).value.match(pattern))
      alert("Success");
      else
      alert("Failure");
      }
      

      I added some extra debug stuff and it comes up with the failure alert

        Two things: I forgot you need a "g" modifier at the end of the regex to make it global, plus you do not need to quote the pattern. So this little script worked OK for me:

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
        <html lang='en'>
        <head>
        <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
        <title>Untitled</title>
        <script type="text/javascript">
        function removeBBcode(areaId)
        {
           var text1 = new String("[quote]hello[/quote]");
           var pattern = new RegExp(/\[[^\]]*\]/g);
           var text2 = new String(text1.replace(pattern,""));
           alert('Before: '+text1);
           alert('After: '+text2);
        }
        </script>
        </head>
        <body onload="removeBBcode('foobar');">
        
        </body>
        </html>
        

          It was probably the damn quotes.......

          Many many thanks........

          I bow to your superior knowledge 😃

            Don't know about "superior"...It's more a matter of curiosity and stubbornness. 🙂

              Write a Reply...