|
-
June 25th, 2009, 05:35 PM
#1
[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.
-
June 25th, 2009, 08:16 PM
#2
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.
-
June 26th, 2009, 08:49 AM
#3
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");
}
-
June 26th, 2009, 11:00 PM
#4
Re: Regex Help (location.search)
 Originally Posted by PeejAvery
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.
-
July 1st, 2009, 12:51 PM
#5
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
-
July 5th, 2009, 04:55 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|