2

Is there a way to obtain a URL parameter in a case insensitive way using jquery?

Essentially, I'm looking to do something like $.url('?someparameter');, where it would match both http:\\www.test.com?someparameter=ABC or
http:\\www.test.com?SOMEparAMeter=ABC

A1rPun
  • 16,287
  • 7
  • 57
  • 90
Filip
  • 81
  • 1
  • 5

3 Answers3

3

You should try toLowerCase. This function converts any string to lowercase.

1

Use a regular expression where you set the case-insensitive flag.

Regular Expressions -- scroll down to "Advanced Searching With Flags"

Please take a look at: How can I get query string values in JavaScript?

The line to adapt to your needs is as follows:

var regex = new RegExp("[\\?&]" + name + "=([^&#]*)", "i");
//"i" for case-insensitive
Community
  • 1
  • 1
PeterKA
  • 24,158
  • 5
  • 26
  • 48
0

This doesnt use jQuery, just javascript. But it addresses the question in general.

The problem w/ ucasing the entire ULR is you may be keying off the value to look up an HTML element.

why there is not a collection of keys in URL.searchParams, I do not know, but there is not. Below is a function i wrote that will find a key and return a value.

I am just barely literate in regEx, so I am sure there is a better regEx that can pull the value out and omit trailing key value pairs.

 function getParm_CI(parm) { 
                var str = window.location.href;
                 var rgx = new RegExp('\\b' + parm + '=.*\\b', 'gi');

                //this gets an array of matches
                var aMatches = str.match(rgx);
                if (aMatches == null) return;
                var parmVal = aMatches[0].substring(parm.length + 1);

                        //we shouldnt, but make sure there are not trailing parms
                var idx = parmVal.indexOf('&');
                        //alert('amp:' + idx);
                if (idx > -1) parmVal = parmVal.substring(0, idx);
                return parmVal;
        }

usage would be like this

var topic = getParm_CI('SOMEparAMeter');
greg
  • 1,673
  • 1
  • 17
  • 30