SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Land of 10k Lakes
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RegExp match and replace problem

    Hi All,

    I have this function that I'm using to preview a forum post. I'm trying to get it to remove the Break tags WITHIN the code blocks and not BETWEEN the code blocks and cant seem to find the right RegExp expression to do it. As you can see I'm converting the code blocks into a textarea input field much like it does here at Sitepoint but when someone presses the "enter/return" key a break tag is inserted that when rendered might be confusing to the poster when they preview the post. I'm a Classic ASP developer and only have a little experience with javascript, could someone please help?

    Code:
    function preview (id){
     //var text = document.reply.message.value;
    var text = "['code]Some Code['/code]<BR>['code]Some<BR>More<BR>Code['/code]";
     text = String(text);
     var patt1 = new RegExp(/((\[code\])*(\<BR\>)*(\[\/code\])*)*/g);
     
      if(document.getElementById(id).style.display =='none'){ 
       document.getElementById(id).style.display ='inline';
      var result = patt1.test(text);
      if (result == true){
       //text = text.match(patt1);
          text = String(text);
       text = text.replace(/\B\<BR\>/g,"");
       text = text.replace(/\[code\]/g,"<textarea rows=5 cols=20>");
       text = text.replace(/\[\/code\]/g,"</textarea>");
       text = text.replace(/\,/,"<br />");
      }
      else{
        text = text.replace(/\[code\]/g,"<textarea rows=5 cols=20>");
        text = text.replace(/\[\/code\]/g,"</textarea>");
       text = text.replace(/\B\<BR\>/g,"");
       text = text.replace(/\,/,"<br />");
      }
      document.getElementById(id).innerHTML = text;
     }
     else {
       document.getElementById(id).style.display = 'none';
     }
    }
    Note: I had to add single quote marks in the "code" tags of the text string to avoid sitepoint rendering them as textarea boxes.

    tia

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Land of 10k Lakes
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No Gurus here that can help me out?

    Can this even be done?

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The following seems to work:
    Code:
    var str = '[code]Some<BR> Code[/code]<BR>[code]Some<BR>More<BR>Code[/code]';
    var newstr = str.replace(/(.*?)((<BR>)?(?!(\[code\])))/g, "$1");

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Land of 10k Lakes
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does work! Thanks Much!!


Bookmarks

Posting Permissions

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