RegExp match giving me a null, while plain match works fine.

Discussion in 'JavaScript' started by kou, Jun 21, 2009.

  1. #1
    Hello,
    I'm trying to select a word and around the 200 characters of it from a web page,
    and send it to my local server via a bookmarklet.

    but something is not working, and giving me a null.

    here's my test code(for firefox)

    javascript:var tx=document.body.innerHTML;var tx=tx.replace(/<[^>]*>/g,"");var wd=document.getSelection();var reg=new RegExp('.{100}' + wd + '.{100}');var sen=tx.match(reg);alert(sen);
    Code (markup):
    when I try this code, say at this website.
    http://en.wikipedia.org/wiki/Cartman%27s_Mom_Is_a_Dirty_Slut

    words like 'episode' and 'Learning' giving me a null
    but when I select 'father' or 'apparent' and click the bookmarklet,
    it works fine.
    I'm wondering what causes this problem.

    Could someone tell me what is wrong with my code?
    Any help would be appreciated. thanks.

    P.S.
    here's plain match version;
    javascript:var tx=document.body.innerHTML;var tx=tx.replace(/<[^>]*>/g,"");var sen=tx.match(/.{10}episode.{10}/);alert(sen);
    Code (markup):
    This code works with any word(in this code 'episode') in a web page.
    But you have to change the reqular expression manually, and that is not an option.
     
    kou, Jun 21, 2009 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You're re-declaring tx
     
    Logic Ali, Jun 21, 2009 IP
  3. kou

    kou Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I fix that.

    here's my latest test code(firefox)
    javascript:var tx=document.body.innerHTML;tx=tx.replace(/<.+?>/g,"");tx=tx.replace(/\n/g," ");var wd=document.getSelection();var reg=new RegExp('.{100}' + wd + '.{100}');var sen=tx.match(reg);alert(sen[0]);
    
    Code (markup):
    This works fine. I think I solved the problem.
    I'm sure there are some redundancy, but it'll do.
    If you have any suggestions, feel free to let me know.
    thanks!
     
    kou, Jun 21, 2009 IP