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 4 of 4
  1. #1
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts

    Replace() dilemma

    Hi everybody

    I am wanting to replace the Break tags "<br>" in a string.
    All is well with a single break tag but if I have two together "<br><br>" then only one is replaced.

    I am using the following code

    <script>
    function mailme(){
    txt="This<br>is<br><br>a<br>test"
    pattern = /\<br>\b/ig;
    newString = txt.replace(pattern,"%0D%0A");
    location="mailto:yourname@youraddress.com?Body="+newString
    }
    </script>

    <a href="#null" onclick="mailme()">Mail Me</a>

    What I should get is an empty line between "is" and "a"

    This
    is

    a
    test

    What I actually get when two br tags are together is

    This
    is<br>
    a
    test


    note the break tag

    I am not really up on reg expressions so I need a bit of help here

    Thank you

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem is that the regular expression looks for a <br> tag with a word boundary after it, so if there are two together only one will be removed. Try this:

    Code:
    var pattern =  /\<br\>/gi;
    Hope that helps!

    Happy coding!

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    nolachrymose

    Thank you very much for your help it certainly did the trick.

    I must find the time to learn more about regular expressions but for now, once again, thank you

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    nolachrymose

    I also found that this worked

    pattern = /\<br>/ig;

    when you mentioned the word boundary I thought "hmm....is that what the \b is for" so I took out the \b from mine and thats how I found it worked, unless it is still not the right way to code it

    but once again thanks


  •  

    Posting Permissions

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