extracting string in javascript

Discussion in 'JavaScript' started by cyberpine, Dec 8, 2009.

  1. #1
    From this string:
    "(EVAL)(H:somestring)Other Text here"

    I need to extract "(H:somestring)" and "somestring" into variables where somestring will could be and set of characters.

    Below is not working.


    
    <script type="text/javascript">
    var x = "(EVAL)(H:somestring)Some other Text here";
    var full =(x.match(/\(H\:(.*?)\)/g));	
    alert(full);
    var inside = (x.match(/\(H\:(.*?)\)/g)); // not sure how
    alert(inside);
    </script>
    
    Code (markup):
    Thanks for any help or information.
     
    Last edited: Dec 8, 2009
    cyberpine, Dec 8, 2009 IP
  2. The Chosen Generl

    The Chosen Generl Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just remove that question mark in the expression.
     
    The Chosen Generl, Dec 8, 2009 IP
  3. cyberpine

    cyberpine Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm on IE 7.

    I tried this and it did not work

    
    <script type="text/javascript">
    var x = "(EVAL)(H:somestring)Some other Text here";
    var full =(x.match(/\(H\:(.*?)\)/g));	
    alert(full);
    var inside = (x.match(/\(H\:(.*)\)/g)); // without question mark
    alert(inside);
    </script>
    
    Code (markup):
    Both produce "(H:somestring)"

    However, this (aparently without /g for greedy?

    
    var inside = (x.match(/\(H\:(.*?)\)/)); // not sure how
    
    Code (markup):
    produces "(H:\somestring),somestring"

    How do I address the second one?

    thanks.
     
    cyberpine, Dec 8, 2009 IP
  4. unigogo

    unigogo Peon

    Messages:
    286
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I suggest you test your regexp with the online regexp tester.

    Your regexp has returned both (H:somestring)" and "somestring". (H:somestring) can be extracted in the return of match method. "somestring" submatch can be extracted with RegExp.$1.
     
    unigogo, Dec 12, 2009 IP