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?

asked Aug 30, 2014 at 21:10
4
  • 1
    Perhaps you should start with extracting all of the parameters and afterwards deal with the case insensitivity - perhaps with a call to map() to iterate over each of the parameters. Commented Aug 30, 2014 at 21:12
  • I might be wrong, but this looks like it's already case insensitive... Commented Aug 30, 2014 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. Commented Aug 30, 2014 at 21:17
  • Ah, didn't know you could do that in the new RegEx() call. Cool. Commented Aug 30, 2014 at 21:18

2 Answers 2

3

Add i flag for regexp(more info):

new RegExp('your regexp', 'i')
answered Aug 30, 2014 at 21:17

1 Comment

You don't even need new.
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!

answered Aug 30, 2014 at 21:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.