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.
Related
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
Given
google.com
How can I get .com and save it to a variable. I was thinking of using regex to split google.com into google and ".com", but I don't know the regex to do this.
It might help to know that I got the hostname from using window.location.hostname
Thank you!
You can split the location by ., and the last value in the array will be the extension. Something like this would work:
var extension=location.hostname.split(".");
extension=extension[extension.length-1];
console.log(extension)
JS Fiddle Example: https://jsfiddle.net/igor_9000/yvy0zrat/
Hope that helps!
You could use a substring and lastIndexof:
var str = window.location.hostname;
var ending = str.substring(str.lastIndexOf(".")+1);
https://jsfiddle.net/06r86q0n/
Mostly the same as the other answer, but neither helps with double domains like ".co.uk"
If you are looking to create a regex statement that will only grab the .com then I would use something like:
/(.com)/ as your Regex statement
so your code would look like something like this:
var regexMatch = /(.com)/; as a regular expression literal
or
var regexMatch = new RegExp(".com"); as constructor function of the RegExp object.
Do not use split, its a bit slower than slice:
var last = location.hostname
var t = last.slice(last.lastIndexOf("."));
console.log(t) // .com, .net etc...
Fiddle: https://jsfiddle.net/1qu0rffb/
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>");
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;
I have a list of urls like this:
http://www.mylocal.com
http://v1.mylocal.com
http://v2.mylocal.com
http://www.mylocal2.com
http://www.mylocal3.com
And I want to write a JS that if I define the search string be "*.mylocal.com" , then it will return www.mylocal.com v1.mylocal.com and v2.myloca.com. And if the search string is "www.local.com", then it will return only www.mylocal.com
how should I write it?
The following regex will match what you want when given a host string:
var reg = new RegExp('^https?://([^.]*' + host + ')');
So, for example:
var host = '.mylocal.com';
reg.exec('http://www.mylocal.com'); // ["http://www.mylocal.com", "www.mylocal.com"]
reg.exec('http://v1.mylocal.com/path'); // ["http://v1.mylocal.com", "v1.mylocal.com"]
reg.exec('https://v3.mylocal.com'); // ["https://v3.mylocal.com", "v3.mylocal.com"]
host = 'www.mylocal.com';
reg.exec('http://www.mylocal.com'); // ["http://www.mylocal.com", "www.mylocal.com"]
reg.exec('http://v1.mylocal.com/path'); // null
reg.exec('https://v3.mylocal.com'); // null
You could also refer to the following post for a full URI regex:
Regular expression validation for URL in ASP.net.
If you want to search on each part of the URL then do just that.
split the URL into 3 searching strings, then run a match of each against your split search terms, this way you can control matching at the beginning and end of each term, and if you would like can order the rest of the terms appropriately.