0

I have the following function (which I didn't write) to extract a URL parameter value:

function getURLParameter(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null
}

I have virtually no experience with regular expressions. This code currently does a case sensitive search for the parameter name. I'd like to have RegExp do a case insensitive search for the name of the parameter. Could someone show me how I might change this to accomplish that?

Randy Minder
  • 47,200
  • 49
  • 204
  • 358
  • 1
    Perhaps you should start with [extracting all of the parameters](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) and afterwards deal with the case insensitivity - perhaps with a call to `map()` to iterate over each of the parameters. – Lix Aug 30 '14 at 21:12
  • I might be wrong, but this looks like it's already case insensitive... – jwatts1980 Aug 30 '14 at 21:14
  • 1
    Look at RegExp modifiers given as second parameter to `RegExp`. You can give `'i'` as second parameter to `new RegExp` to perform an case insensitive search. – nisargjhaveri Aug 30 '14 at 21:17
  • Ah, didn't know you could do that in the `new RegEx()` call. Cool. – jwatts1980 Aug 30 '14 at 21:18

2 Answers2

3

Add i flag for regexp(more info):

new RegExp('your regexp', 'i')
maximkou
  • 5,252
  • 1
  • 20
  • 41
0

Here's something that I've been using that may help, as I need to do something very similar to pull down a substring of the current page's URL to then pass into a variable to be used in several of my functions.

Here's the generic format of my URLs:

file:///Users/myname/folder/teamname.html

And here's what how I'm parsing them:

function parseURL() {
  var match = window.location.href.match(/(\w+).html$/);
  if (match) {
    return match[1];
  }
  return null;
}

This will do this:

1) Check the URL for the current page 2) Parse the URL into two different fragments of an array: "teamname" and "html" 3) I then return match[1] which is "teamname"

How I'm using it:

From there, I declare a variable for the parseURL function like this:

var teamSched = parseURL();

So now, I can make dynamic calls for any page with the same URL syntax I've outlined above to have specific code executed with the page-specific variable from parseURL(). Then, I use that variable to generate unique datasets from objects in my code who's key match the "team name" variable created by parseURL().

Someone definitely correct me if I'm wrong, but case sensitivity shouldn't be a factor here, as long as the value you're pulling from your URL via parseURL matched the variable, object key, etc. you're trying to access.

I hope that helps!

RTAlamo
  • 5
  • 1
  • 4