Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: multi-browser javascript word replace

  1. #1
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default multi-browser javascript word replace

    can some one please write a script that replaces all "5" to "five" and "4" to "four" in a webpage? please note that replacing "4" and "5" are just examples I had a script before but I went on my site on internet explorer and it wasn't changing the words. It only worked in firefox. this is the script i had before:

    Code:
    var replacements, regex, key, textnodes, node, s;
    
    replacements = {
    
        "\u201cv": "c",
        "\u201c": '"',
        "\u201d": '"',
        "\u2026": "...",
        "\u2002": " ",
        "\u2003": " ",
        "\u2009": " ",
        "\u2013": "-",
        "\u2014": "--",
        "\u2122": "(tm)"};
    regex = {};
    for (key in replacements) {
        regex[key] = new RegExp(key, 'g');
    }
    
    textnodes = document.evaluate(
        "//text()",
        document,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null);
    for (var i = 0; i < textnodes.snapshotLength; i++) {
        node = textnodes.snapshotItem(i);
        s = node.data;
        for (key in replacements) {
            s = s.replace(regex[key], replacements[key]);
        }
        node.data = s;
    }
    (script from dive into gease monkey)

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,951
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Version 5 Browser Compatible Text Conversion Script</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    if (document.documentElement && document.documentElement.childNodes)
    window.onload = function() {
    var tn = [], grabTextNodes = function(n) {
    for (var i = n.length - 1; i > -1; --i)
    if (n[i].nodeName != '#text' && n[i].childNodes)
    grabTextNodes(n[i].childNodes);
    else
    tn[tn.length] = n[i];
    }, replacements = {
    
        "\u00a9": "(c)",
        "\u201c": '"',
        "\u201d": '"',
        "\u2026": "...",
        "\u2002": " ",
        "\u2003": " ",
        "\u2009": " ",
        "\u2013": "-",
        "\u2014": "--",
        "\u2018": "'",
        "\u2019": "'",
        "\u2122": "(tm)",
        "5"     : "five"
        
        }, regex = {};
    
    grabTextNodes(document.body.childNodes);
    
    for (var key in replacements) {
    regex[key] = new RegExp(key, 'g');
    for (var i = tn.length - 1; i > -1; --i)
    tn[i].nodeValue = tn[i].nodeValue.replace(regex[key], replacements[key]);
    };
    };
    </script>
    </head>
    <body>
    “Hi Bob” ™  5
    <div>
    ‘The Last Title’
    </div>
    </body>
    </html>
    Last edited by jscheuer1; 06-16-2008 at 08:07 AM. Reason: key was informally global, corrected (addition red)
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    nhoss2 (06-16-2008)

  4. #3
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    oh my god, thank you sooooooo much!! i though no one was going to reply

    EDIT: last request, which is just adding one more line of code, which is replacing . (fullstop) with a (space). i tried putting the . in between the two quotation marks, but that ended up replacing everything on the page..
    Last edited by nhoss2; 06-16-2008 at 08:30 AM.

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,951
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    I'm not sure exactly what you mean but, a period (.) in a regular expression matches any character. If you want it to only match a period, you need to escape it:

    Code:
    \.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. The Following User Says Thank You to jscheuer1 For This Useful Post:

    nhoss2 (06-16-2008)

  7. #5
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    yea, im a bit crap at explaning things, but i think you just solved my problem. thanks youre a legend

  8. #6
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    ugh, it didnt work.. what i wanted to do was replace a full stop with a space. i put
    Code:
    "\.": " ",
    but that didnt work

  9. #7
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Because your regexps are constructed from a string you need to escape the backslash itself as well:
    Code:
    "\\."    : " "

  10. The Following User Says Thank You to DimX For This Useful Post:

    nhoss2 (06-18-2008)

  11. #8
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    oh, right. thanks alot, you guys are really helpfull

    EDIT:it worked, but it only replaced one fullstop, how can i reaplce all fullstops on the page?
    Last edited by nhoss2; 06-18-2008 at 01:48 AM.

  12. #9
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    It replaces all fullstops for me.
    Make sure that 'g' is there: regex[key] = new RegExp(key, 'g'); and that you put a comma after the replacement which precedes "\\." : " ":
    Code:
    ...,
    "5"     : "five",
    "\\."   : " "

  13. #10
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I am try to use the code above to replace the words of normal body words in bold but instead I defend my words bold word appears <b>example</ b>.

    I can tell someone how to solve this problem and instead of <b>example</ b> to defend my example ?

    Thanks.

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
  •