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 2 of 2

Thread: RegEx help

  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question RegEx help

    I'm trying to use a regex in javascript to match and replace text in a web page but it's not matching. I think it may have something to do with the extra white space. I can get it to match some snippets from the code but once I get to the white space it dies. What am I doing wrong?

    Code to be matched:
    Code:
    <td bgcolor="#E1E1E1">
    &nbsp;</td>
    Desired replacement code:
    Code:
    <td bgcolor="#E1E1E1">lasers!</td>
    Regex code:
    Code:
    var clipRE = /<td bgcolor="#E1E1E1">&nbsp;<\/td>/g;
    var clipNew = "<td bgcolor=\"#E1E1E1\">lasers!<\/td>";
    document.getElementsByTagName('body')[0].innerHTML =
    document.getElementsByTagName('body')[0].innerHTML.replace(clipRE, clipNew);

  • #2
    New Coder
    Join Date
    Dec 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Further investigation yields something strange. It seems to be choking on the letter E...... var clipRE = /1">\s*&nbsp;<\/td>/g; will match but var clipRE = /E1">\s*&nbsp;<\/td>/g; will not even though the code has an E immediately preceding the 1.

    This code works:
    Code:
    var clipRE = /1">\s*&nbsp;<\/td>/g;
    var clipNew = "1\">lasers!<\/td>";
    document.getElementsByTagName('body')[0].innerHTML =
    document.getElementsByTagName('body')[0].innerHTML.replace(clipRE, clipNew);
    This code does not:
    Code:
    var clipRE = /E1">\s*&nbsp;<\/td>/g;
    var clipNew = "E1\">lasers!<\/td>";
    document.getElementsByTagName('body')[0].innerHTML =
    document.getElementsByTagName('body')[0].innerHTML.replace(clipRE, clipNew);


  •  

    Tags for this Thread

    Posting Permissions

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