SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    Trash Boat mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Javascript Regex

    Im very bad when it comes to javascript.

    Anyone know how to get the keyword from a google url

    Example url
    Code:
    http://www.google.com/search?q=javascript+regex+examples&ie=utf
    want to extract javascript+regex+examples
    in php would use

    PHP Code:
    $url 'http://www.google.com/search?q=javascript+regex+examples&ie=utf';
    $phrase preg_match("/q=(.*?)&/",$url$m);
    echo 
    $m[1]; 
    Anyone know how to use that regex in Javascript?

    Thanks

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Check out match and exec. Give it a go and if you encounter problems post what you attempted.

  3. #3
    Trash Boat mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey that works awesome used

    Code:
    <script type="text/javascript">
    str = "http://www.google.com/search?q=javascript+regex+examples&ie=utf";
    re = /q=(.*?)&/i;
    found = str.match(re);
    document.write(found);
    </script>
    It outputs
    q=javascript+regex+examples&,javascript+regex+examples

    You guys know how just show it 1x?

    Thanks for the reply man!

  4. #4
    Trash Boat mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey this works!

    Code:
    <script type="text/javascript">
    var myRe = /q=(.*?)&/g;
    var str = "http://www.google.com/search?q=javascript+regex+examples&ie=utf";
    var myArray;
    while ((myArray = myRe.exec(str)) != null)
    {
      var msg = myArray[0] ;
      alert(msg);
    }
    </script>
    Thanks so much

    It alerts
    q=javascript+regex+examples&

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    What you're getting there is the full match.

    If you only want the captured group, use myArray[1] instead for
    javascript+regex+examples
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    Trash Boat mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The things you can find out online.

    Never fails to amaze me lol

    Hey thanks so much for that last adjustment

    works 100% awesome now


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
  •