CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2006
    Posts
    306

    [RESOLVED] Regex Help (location.search)

    Alright, I need help with some regular expressions. I'm trying to get all the GET parameters within javascript in one fell swoop, but can't. I'm wondering if you can help me with this.

    Code:
    //location.search = "?Name=Bob&Time=401&PM=0";
    location.search.match(/([0-9a-zA-Z]+(\=)[0-9a-zA-Z]+)+/g)
    Now, this is the result.
    Code:
    ["Name=Bob", "Time=401", "PM=0"]
    Is there a way to make the expression return something like this?
    Code:
    ["Name", "Bob", "Time", "401", "PM", "0"]
    I tried adding more parentheses but it's not working.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Regex Help (location.search)

    Why bother when you can create them into an associative array very simply?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Apr 2009
    Posts
    598

    Re: Regex Help (location.search)

    Just for historical culture, here is my very old solution, working with or without substring:
    Code:
    function GetOneParam(name)
    {
    	var start=location.search.indexOf("?"+name+"=");
    	if (start<0) start=location.search.indexOf("&"+name+"=");
    	if (start<0) return '';
    	start += name.length+2;
    	var end=location.search.indexOf("&",start)-1;
    	if (end<0) end=location.search.length;
    //	var result=location.search.substring(start,end);
    	var result='';
    	for(var i=start;i<=end;i++) {
    		var c=location.search.charAt(i);
    		result=result+(c=='+'?' ':c);
    	}
    	return unescape(result);
    }
    
    function GetAllParam()
    {
       var v_Name=GetOneParam("Name");
       var v_Time=GetOneParam("Time");
       var v_PM=GetOneParam("PM");
    }

  4. #4
    Join Date
    May 2006
    Posts
    306

    Re: Regex Help (location.search)

    Quote Originally Posted by PeejAvery View Post
    Why bother when you can create them into an associative array very simply?
    I had something like that.

    It was this.
    Code:
    var arr = location.search.match(/([0-9a-zA-Z]+)=([0-9a-zA-Z]+)+/g).join().split(/[,=]/g);
    var GET = {};
    for (var i = 0; i < arr.length; i += 2)
    {
        GET[arr[i]] = arr[i + 1];
    }
    Just wondering if there was a shorter way, but I think this will suffice.
    Last edited by code?; June 26th, 2009 at 11:04 PM.

  5. #5
    Join Date
    Jul 2009
    Posts
    3

    Re: Regex Help (location.search)

    Code:
    <script type="text/javascript">
    	
    	//search = location.search 
    	search = "?Name=Bob&Time=401&PM=0";
    	a = search.replace('?', "").split(/[,(&|=)]/ig);
    	document.write(a);
    
    </script>
    output [array]: Name,Bob,Time,401,PM,0

  6. #6
    Join Date
    May 2006
    Posts
    306

    Re: Regex Help (location.search)

    I've got it all figured out now, thanks!

    I got it all down to one line.

    Code:
    var $_GET = eval("({" + location.search.substring(1).replace(/=/g, ":").replace(/&/g, ",").replace(/([0-9a-zA-Z]+):([0-9a-zA-Z]+)/g, "'$1':'$2'") + "})");
    It could use some more improvement (replacing "=" and "&" all in one call), but it works just how I want it to.

    Better yet, replace everything in one "replace" call.

    Well, all I wanted was some way to get it down into one line, and I did.

    Thanks for all your help.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured