Javascript URL Regex That Checks Regex with URL - javascript

I have this URL...
http://www.google.com/local/add/analytics?hl=en-US&gl=US
And I want to check these URLs to see if they matches above URL...
www.google.com/local/add*
www.google.com/local/add/*
http://www.google.com/local/add*
http://www.google.com/local/add/*
https://www.google.com/local/add*
https://www.google.com/local/add/*
You can see the input URL is also a regex having * so what regex that I can use to match a list of URLs with a regex to see if the url exists? Currently I am doing this...
var isAllowed = (url.indexOf(newURL) === 0);
Which is definitely not efficient.

it's not the cleanest regex i've ever written but I think it should work.
var url = "http://www.google.com/local/add/analytics?hl=en-US&gl=US";
var reg = /((https|http|)(\:\/\/|)www\.google.com\/local\/add(\/|)).*/;
console.log(reg.test(url));
this will return true for all of these cases
www.google.com/local/add*
www.google.com/local/add/*
http://www.google.com/local/add*
http://www.google.com/local/add/*
https://www.google.com/local/add*
https://www.google.com/local/add/*
it should look for (http or https or nothing) then (:// or nothing) then www.google.com/local/add then (/ or nothing) then anything.
the one case it will also return true that I will leave for you is the case (http|https)www.google.com/local/add(/|)*

var reg = new RegExp("(https?://)?(www.)?google.com/local/add/?"),
URL = "http://www.google.com/local/add/analytics?hl=en-US&gl=US";
console.log(reg.test(URL));
I've used the ? a lot, which means, whatever character precedes the question mark may or may not be matched.
https? means the s may or may not be there. (www.)? means that the www. may be absent entirely. You hopefully get how it works now.
Demo
Learn how to use Regular Expressions

As far as I understand you, you want something like this:
Convert the input URL to a regex. E.g.:
var input = "http://www.google.com/local/add*";
var reg_url = input .replace(/\*/g,".*").replace(/\./g,"\\.");
you might need to escape some more characters, see here
And check if it matches:
var url = "http://www.google.com/local/add/analytics?hl=en-US&gl=US";
var isAllowed = url.search(reg_url) >= 0;

Related

Is there a more succinct way to get the last number in my url?

So I currently pass two variables into the url for use on another page. I get the last variable (ie #12345) with location.hash. Then from the other part of the url (john%20jacob%202) all I need is the '2'. I've got it working but feel there must be a cleaner and succinct way to handle this. The (john%20jacob%202) will change all the time to have different string lengths.
url: http://localhost/index.html?john%20jacob%202?#12345
<script>
var hashUrl = location.hash.replace("?","");
// function here to use this data
var fullUrl = window.location.href;
var urlSplit = fullUrl.split('?');
var justName = urlSplit[1];
var nameSplit = justName.split('%20');
var justNumber = nameSplit[2];
// function here to use this data
</script>
A really quick one-liner could be something like:
let url = 'http://localhost/index.html?john%20jacob%202?#12345';
url.split('?')[1].split('').pop();
// returns '2'
How about something like
decodeURI(window.location.search).replace(/\D/g, '')
Since your window.location.search is URI encoded we start by decoding it. Then replace everything that is not a number with nothing. For your particular URL it will return 2
Edit for clarity:
Your example location http://localhost/index.html?john%20jacob%202?#12345 consists of several parts, but the interesting one here is the part after the ? and before the #.
In Javascript this interesting part, the query string (or search), is available through window.location.search. For your specific location window.location.search will return ?john%20jacob%202?.
The %20 is a URI encoded space. To decode (ie. remove) all the URI encodings I first run the search string through the decodeURI function. Then I replace everything that is not a number in that string with an empty string using a regular expression.
The regular expression /\D/ matches any character that is not a number, and the g is a modifier specifying that I want to match everything (not just stop after the first match), resulting in 2.
If you know you are always after a tag, you could replace everything up until the "#"
url.replace(/^.+#/, '');
Alternatively, this regex will match the last numbers in your URL:
url.match(/(?<=\D)\d+$/);
//(positive look behind for any non-digit) one more digits until the end of the string

Match Url path without query string

I would like to match a path in a Url, but ignoring the querystring.
The regex should include an optional trailing slash before the querystring.
Example urls that should give a valid match:
/path/?a=123&b=123
/path?a=123&b=123
So the string '/path' should match either of the above urls.
I have tried the following regex: (/path[^?]+).*
But this will only match urls like the first example above: /path/?a=123&b=123
Any idea how i would go about getting it to match the second example without the trailing slash as well?
Regex is a requirement.
No need for regexp:
url.split("?")[0];
If you really need it, then try this:
\/path\?*.*
EDIT Actually the most precise regexp should be:
^(\/path)(\/?\?{0}|\/?\?{1}.*)$
because you want to match either /path or /path/ or /path?something or /path/?something and nothing else. Note that ? means "at most one" while \? means a question mark.
BTW: What kind of routing library does not handle query strings?? I suggest using something else.
http://jsfiddle.net/bJcX3/
var re = /(\/?[^?]*?)\?.*/;
var p1 = "/path/to/something/?a=123&b=123";
var p2 = "/path/to/something/else?a=123&b=123";
var p1_matches = p1.match(re);
var p2_matches = p2.match(re);
document.write(p1_matches[1] + "<br>");
document.write(p2_matches[1] + "<br>");

Regex to detect urls with '?' character at the end

I found many solutions, but none was useful for me.
Let's say, as an example, I want to find URLs that start with www. and end with a space or ?. In this case, I really mean it ends in a ?, not that it's necessarily a CGI-related URL.
I'm trying to use the regex
var r = /(^|[\s\?])(www\..+?(?=([\s]|\?|($))))/g;
My sample use: http://jsfiddle.net/DKNat/2/
How can I use \? in a regex to prevent the end of the URL containing / before ??
http://jsfiddle.net/DKNat/11/
I can't solve last prob with DOT at the end of url.
Can any body help?
Try this in your fiddle:
var r = /(^|\??)(www\.[^\?]+)/g;
I updated your fiddle here:
http://jsfiddle.net/DKNat/3/
Update:
I see what you are trying to do now. Unfortunately, both your strings are essentially the same, apart from the /, so unless you want your regex to make the assumption that a ? anywhere after a slash denotes a CGI call, then there isn't much you can do. But you could try this:
var r = /(^|\??)(www\.[^\?]+\/[^\/]+\?[^\?]+|www\.[^\?]+)/g;
Updated fiddle:
http://jsfiddle.net/DKNat/5/
Update 2: After determining the requirements, this is the final RegExp I added to fiddle 10:
var r = /(^|[\?\s])(www\.[^\? ]+\/[^\/ ]*\?[^\? ]+|www\.[^\? ]+)/g;

Can regex matches in javascript match any word after an equal operator?

I am trying to target ?state=wildcard in this statement :
?state=uncompleted&dancing=yes
I would like to target the entire line ?state=uncomplete, but also allow it to find whatever word would be after the = operator. So uncomplete could also be completed, unscheduled, or what have you.
A caveat I am having is granted I could target the wildcard before the ampersand, but what if there is no ampersand and the param state is by itself?
Try this regular expression:
var regex = /\?state=([^&]+)/;
var match = '?state=uncompleted&dancing=yes'.match(regex);
match; // => ["?state=uncompleted", "uncompleted"]
It will match every character after the string "\?state=" except an ampersand, all the way to the end of the string, if necessary.
Alternative regex: /\?state=(.+?)(?:&|$)/
It will match everything up to the first & char or the end of the string
IMHO, you don't need regex here. As we all know, regexes tend to be slow, especially when using look aheads. Why not do something like this:
var URI = '?state=done&user=ME'.split('&');
var passedVals = [];
This gives us ['?state=done','user=ME'], now just do a for loop:
for (var i=0;i<URI.length;i++)
{
passedVals.push(URI[i].split('=')[1]);
}
Passed Vals wil contain whatever you need. The added benefit of this is that you can parse a request into an Object:
var URI = 'state=done&user=ME'.split('&');
var urlObjects ={};
for (var i=0;i<URI.length;i++)
{
urlObjects[URI[i].split('=')[0]] = URI[i].split('=')[1];
}
I left out the '?' at the start of the string, because a simple .replace('?','') can fix that easily...
You can match as many characters that are not a &. If there aren't any &s at all, that will of course also work:
/(\?state=[^&]+)/.exec("?state=uncompleted");
/(\?state=[^&]+)/.exec("?state=uncompleted&a=1");
// both: ["?state=uncompleted", "?state=uncompleted"]

How do I validate a URL path with a wildcard in Javascript?

str = 'http://*.foo.com/bar/' is a valid string.
How do I write a regex to validate this in JavaScript?
`http://xyz.foo.com/bar/` ✓ valid
`http://xyz.foo.com/bar/abc/` ✗ invalid
`http://xyz.foo.com/` ✗ invalid
try playing around at RegExr. It has a lot of good information and will give you a javascript regex at the bottom of the page when you are done.
Try this:
var url = // your url
url.match(/http://[a-zA-Z_0-9]+\.foo\.com/bar/$/g)
The $ matches the end of a string. The $ at the end will make sure that there is no text after it.
Can you give some more details?
e,g, /^http://[a-zA-Z]+.foo.com/bar/$/g
will do what you're looking for if it is on a single line (through ^$ delimiters). It will match xyz but not xyz1 which is easy to fix if you want to include numbers.
Play around it and let me know if you have more questions.
You can assign some of the location object's properties to your own variables once the page loads (since location is free to change afterward):
var hostURL = location.host; //should be '*.foo.com'
var pathURL = location.pathname; //should be '/bar'
Then create a RegExp object:
var regex = '.*\.foo\.com/bar/$';
var testURL = new RegExp(regex);
And test the URL:
if (testURL(hostURL + pathURL)) {
//do something
}
This regex oughta do it for you.
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov)\b
Assign the above to a variable. And test if this pattern matches with the url of your choice using test() method of javascript. Update this to suit your needs if need be.

Categories