#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    293
    Rep Power
    11

    How do I replace br with gr within < > or < /> and whitespace and newlines preserved


    I have a string that may consist of several of these:

    Code:
    < br>
    <br>
    <br >
    <br/>
    <br />
    <br / >
    I want to replace each of the br above with gr.
    I also want to preserve case in the original br. So Br becomes Gr, BR becoms GR (or whatever combination.

    Match only those that start with
    Code:
    <br
    and there may be any number of whitespace before the greater than character, or around the "/" character.

    I do not want whitespace and/or new lines removed.

    Here is an example of how the string could look like:

    Code:
    < br>
    The sun is shining
    <br>
    There is no rain<br />
    <BR     >
    We all need water
    <br  
    >
    The resulting string should look like:

    Code:
    < gr>
    The sun is shining
    <gr>
    There is no rain<gr />
    <GR     >
    We all need water
    <gr  
    >
    How do I do the above with javascript regular expression?
    Last edited by phpweb; April 2nd, 2013 at 03:32 AM.
    BongoBongo
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,988
    Rep Power
    9397
    I would do two expressions, almost identical save one uses a lowercase 'b' and the other an uppercase 'B'. Write the rest of the <br> tag around it so that the stuff before and after the "b" is captured and can be inserted right back into the replacement string.

    Like
    Code:
    /(<\s*)b([rR]\s*/?\s*>)/g -> $1g$2
    If you're wondering about needing to consider the "r", that fortunately doesn't really change between the original and the replacement strings so you don't actually have to worry about it.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    293
    Rep Power
    11
    Thanks for reply... I try this... but the regex seem to fail... no alert box popping up.
    Do you know how to make it work?

    Code:
    var str1 = "First: <br> Second: <br > Third: <Br / > Fourth: <BR />";
    var str2 = "";
    	
    str2 = str1.replace( /(<\s*)b([rR]\s*/?\s*>)/g -> $1g$2 );
    alert(str2);

    Also tried using this:

    Code:
    str2 = str1.replace(/(<\s*)b([rR]\s*/?\s*>)/g, '$1g$2');
    But that line also break the JS, and no alert box.

    I have tested your regExp here:
    http://gskinner.com/RegExr/
    and there it works just fine...

    I do not understand what is wrong with my lines of REPLACE code... Hope someone can tell me what I do wrong?
    Last edited by phpweb; April 2nd, 2013 at 05:16 AM.
    BongoBongo
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    when you don't even get the alert() box, then obviously there's a deeper problem. Check the JavaScript console of your browser, you should see a syntax error, because the slash within the regex literal isn't escaped:

    Code:
    /(<\s*)b([rR]\s*\/?\s*>)/g
    And that "->" of course wasn't literal JavaScript code.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    293
    Rep Power
    11
    When I used new RegExp and "\\s" instead of "\s" it worked...

    Code:
    var str1 = "First: <br> Second: <br > Third: <Br / > Fourth: <BR />";
    var re = new RegExp("(<\\s*)b([rR]\\s*/?\\s*>)","g"); 
    str1 = str1.replace(re,'$1g$2');
    alert(str1);
    Thanks a lot for the regexpression
    BongoBongo

IMN logo majestic logo threadwatch logo seochat tools logo