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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post

    Regular Expression to Replace <br /> tag

    Ok, regular expressions are not my strong suite but I have always been able to figure out what I need in the past. I just can't get my head around this one:

    msg=msg.replace(all instances of <br /> found with the <xmp></xmp> tag,"\n");

    I can't figure out how to change just that one area as I need the <br /> tags to remain everywhere else.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow

    couldn't you just loop round the childNodes of the code block, checking the nodeValue, and if it's a breakline, then replacing it with the newline char?

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Ames, IA, USA
    Posts
    373
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Probably the long way around, but how about something like this:
    Code:
    splitString = msg.split("<xmp>"); // xmp contents plus following
                                      // items in splitString[1]
    xmpString = splitString[1].split("</xmp>"); // xmp contents in xmpString[0]
    newString = xmpString[0].replace(); // insert appropriate regular expression
    msg = splitString[0] + "<xmp>" + newString + "</xmp>" + xmpString[1];
    //  rebuild the message with the altered xmp contents
    This, of course, assumes only one xmp element per message.
    Need more emoticons?
    Visit Catman's Private Stock

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Location
    underground
    Posts
    186
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Here's one way, I guess...

    Code:
    <body>
    <script type="text/javascript">
    function repl(txt)
     {
       var val = txt.value,
            rx = /<xmp>.+<\/xmp>/g,
            ar = val.match(rx),
             i = -1;
       alert(val.replace(rx, function(){return ar[++i].replace(/<br \/>+/g, "\n")}));
     }
    </script>
    <form style="text-align:center">
    <textarea name="txt" cols="50" rows="25">
    <div>
    <br />
    <xmp><br />blah<br />blah<br /></xmp>
    <br />
    </div>
    <br />
    <xmp><br />foo<br />foo<br /></xmp>
    </textarea>
    <br>
    <input type="button" value="test" onclick="repl(txt)">
    <form>
    </body>
    </html>
    The increment isn't really necessary, huh.
    Code:
    function repl(txt)
     {
       var val = txt.value,
            rx = /<xmp>.+<\/xmp>/g,
            ar = val.match(rx);
       alert(val.replace(rx, function(){return ar.shift().replace(/<br \/>+/g, "\n")}));
     }
    Backreferences might be the key to a real solution.
    Last edited by neofibril; 07-09-2004 at 03:51 AM.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks for that. It got me to where I needed to be. It did not seem to grab multiple lines of text though. Lets say the code looked like:

    Code:
    <xmp>
    	p {
    		font-size: 12px;
    	}
    	
    	.p1 {
    		color: white
    	}
    
    	.p2 {
    		color: yellow
    	}
    
    	.p3 {
    		color: #87ceeb;
    	}
    </xmp>
    the .+ was not sufficient, but I got it to work with this regex:

    /<xmp>([\w|\t|\r|\W]*?)<\/xmp>/g

    Your bit with the anonymous function was the last piece in the puzzle. It worked out wonderfully.

  • #6
    Regular Coder
    Join Date
    Jun 2004
    Location
    underground
    Posts
    186
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, I'm no pro with the syntax; originally tried [\s\S]*, but that did too much, so...
    Good topic; I'll use the idea for file system stuff, no doubt.

  • #7
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can we see a working example of how you implemented the expression. I was not sure if were converting the contents of the <xmp> .....</xmp> tags which were on a page or were sending them as a string on a form or something....

    BTW: I had went in thru the innerHTML but was not sure if that was what you wanted, but FWIW, here it is for the archives....

    Code:
    <script>
      function xmp(){
       var xmp = document.getElementsByTagName('xmp');
            alert(xmp[0].innerHTML);
            alert(xmp[0].innerHTML.replace(/<br>/g,'\n'));
            document.forms[0].test.value = xmp[0].innerHTML.replace(/<br>/g,'\\n');
      }     onload = xmp;
    </script></head>
    
    <body>
    <xmp>
    <div id="menu_1" class="menu" style="left:0px">
    <br>
    <a href="target1.html">  Item 1 </a>
    <br>
    <a href="target2.html">  Item 2 </a>
    <br>
    <a href="target3.html">  Item 3 </a>
    <br>
    </div>
    </xmp>
    
    <form>
    <textarea cols="84" rows="12" name="test"></textarea>
    </form>
    Last edited by Willy Duitt; 07-09-2004 at 05:48 AM.

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    For all kinds of reasons that I wish did not exist I have to take plain text from a form and format it with very basic html before it gets sent to another form. Some the text however contains coding examples found with the <xmp> tags. Turning all line breaks into <br /> tags works for every situation I ran into except where the code examples show up. So I ended up needing to replace all the line breaks with <br /> tags except for the cases where the line breaks were contained within the <xmp> tags.

    I know it is confusing, my head still hurts. But this is how I ended up accomplishing the task with neofibril's help:

    Code:
    msg=msg.replace(/\n/g,'<br />');
    rgx=/<xmp>([\w|\t|\r|\W]*?)<\/xmp>/g;
    arr=msg.match(rgx);
    msg=msg.replace(rgx, function(){return arr.shift().replace(/<br \/>+/g, "\n")});


  •  

    Posting Permissions

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