Regular expression to match Google earth URLs - javascript

I am trying to get a regular expression that verifies that the url input a user feeds in a form is a valid google earth url. eg https://earth.google.com/web/#18.2209311,-63.06963893,-0.67163554a,2356.53661597d,34.99999967y,358.1303302h,0t,0r
I have tried to use js and html for verification but its not working here is the regex function that I currently have
var urlRegex = /^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/;
var earthRegex = /earth.google.com/;
and within the function I have this:
if ((linkInUrl !== '-') && !(urlRegex.test(linkInUrl) && earthRegex.test(linkInUrl))) {
return 'Please enter a valid Link URL';

You may try using the following regex pattern:
(?:ht|f)tps?:\/\/earth\.google\.com\/web\/#-?\d+(?:\.\d+)?,-\d+(?:\.\d+)?,-?\d+(?:\.\d+)?a,-?\d+(?:\.\d+)?d,-?\d+(?:\.\d+)?y,-?\d+(?:\.\d+)?h,\d+t,\d+r
Perhaps too long for a full explanation, but each CSV component at the end of the URL is generally matched using something like -?\d+(?:\.\d+)?, which will match a positive/negative integer/float number.

Related

regex email pattern in a negate way

I was working around some regex pattern
I have a variable,
var url = "last_name=Ahuja&something#test.com";
The url contains emailId. I have a regex pattern to check if the variable contains emailId.
var filter = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
My requirement is:
I want exact negate(opposite) of the above pattern. Like, My condition should be false if the url contains email pattern.
I mean the regex pattern should be in that way.
Can somebody please help me on this.
Instead of negating your regex, you can test whether it matches.
var filter = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var url = "last_name=Ahuja&something#test.com";
if(-1 === url.search(filter))
{
alert("Didn't find it");
}
else
{
alert("Found it");
}
Using a negative lookahead you can check if the string does not have an email at the beginning, which mimics the behavior you want since your regex had start and end anchors.
^(?!([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+).*$
But if you wanted to make sure there was no valid email anywhere in the line, you could modify this a little.
^((?!([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+).)*$
See an explanation here
But you should just do this in code like Jonathan suggests.

Validate specific url

I'm making a form for my friend that required all visitor to fill a specific domain name.
What I want is, they've to write a link starting with https://specificname.com and continue with an ID. Example https://specificname.com/id/WordOrNumber . I need something like regex or validator to validate the link they've fill in the form.
This is the string that I am using for now.
case 'url':
if ('http://' === $value) $value = '';
if ($value && !preg_match('/^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:)*#)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(([a-z]|\d|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])*([a-z]|\d|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])))\.)+(([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])*([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|#)+(\/(([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|#)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|#)|[\x{E000}-\x{F8FF}]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|#)|\/|\?)*)?$/iu', $value))
return frmd_error('Please enter a valid URL.', $elm['name']);
break;
This code work perfectly but it will accept all kind of domain name that the visitor fill up.
Please just reply with the regex or validator only because I'll not know what to edit .
I'm bad with coding.
If you want to ensure the URL entered matches a specific prefix, then you can skip the regular expressions and just test the substring, such as:
var validPrefix = 'https://example.com';
if (enteredUrl.substring(0, validPrefix.length) != validPrefix){
alert('Invalid URL.');
}
If you want to allow for minor variations on the URL such as with/without the WWW or http and https, then you can use a simple regex that defines those as optional parts.
var urlRegex = /https?:\/\/(www.)?example.com/;
if (!urlRegex.test(enteredUrl)){
alert('Invalid URL.');
}
If you need to validate the entire URL format rather than just the prefix, the regex will need to be extended based on whatever format criteria you need to match. Some regular expression tutorials should help you determine how to match what you need. To add an ID number after the domain for example you could do:
var urlRegex = /https?:\/\/(www.)?example.com\/\d+/;
if (!urlRegex.test(enteredUrl)){
alert('Invalid URL.');
}
if you prefix and input url is
var validPrefix = "validDomain.com";
var inputURL = "www.validDomain.com/context1";
then all you need to do is
if ( inputURL.indexOf( validPrefix ) == -1 )
{
alert( "No, input URL doesn't contain valid domain prefix" );
}
else
{
alert( "Yes, input URL contains valid domain prefix" );
}

Why is this javascript url validator failing?

I have the following code to validate if a person entered a "valid" url in a textbox:
function validateURL(textval) {
var urlregex = new RegExp(
"^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
return urlregex.test(textval);
}
a user is getting an error where this is returning false for what seems like a valid urL
http://a.website.com/issues/i#browse/TEST-111
Can someone confirm why this example wouldn't pass the "valid url" test?
Can someone confirm why this example wouldn't pass the "valid url" test?
The main trouble with the regex is that www. part is obligatory in the pattern.
If you want to make it optional, use a ? modifier with a group around it ((?:www\.)?):
^(?:(?:(?:ftp|https?):\/\/)?)(?:www\.)?[0-9A-Za-z]+(?:\.[0-9A-Za-z]+)*
This will match http://a.website.com part. To match the whole string, you can use:
^(?:(?:(?:ftp|https?):\/\/)?)(www\.)?[0-9A-Za-z]+(?:\.[0-9A-Za-z]+)*(?:\/[^\/]*)*$
See demo
var re = /^(?:(?:(?:ftp|https?):\/\/)?)(www\.)?[0-9A-Za-z]+(?:\.[0-9A-Za-z]+)*(?:\/[^\/]*)*$/;
var str = 'http://a.website.com/issues/i#browse/TEST-111';
if ((m = re.exec(str)) !== null) {
document.getElementById("res").innerHTML = m[0];
}
<div id="res"/>
Your regex requires that the host name portion starts with www. (this is not a requirement for URLs in general). The URL you are testing does not include www..
There are many other reasons why the regex is broken (you don't test past the first character after www., your attempt to do so bans many characters that are allowed in URLs, etc) but that is why the URL you have isn't passing.

Regex expression to match the First url after a space followed

I want to match the First url followed by a space using regex expression while typing in the input box.
For example :
if I type www.google.com it should be matched only after a space followed by the url
ie www.google.com<SPACE>
Code
$(".site").keyup(function()
{
var site=$(this).val();
var exp = /^http(s?):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/;
var find = site.match(exp);
var url = find? find[0] : null;
if (url === null){
var exp = /[-\w]+(\.[a-z]{2,})+(\S+)?(\/|\/[\w#!:.?+=&%#!\-\/])?/g;
var find = site.match(exp);
url = find? 'http://'+find[0] : null;
}
});
Fiddle
Please help, Thanks in advance
you should be using a better regex to correctly match the query & fragment parts of your url. Have a look here (What is the best regular expression to check if a string is a valid URL?) for a correct IRI/URI structured Regex test.
But here's a rudimentary version:
var regex = /[-\w]+(\.[a-z]{2,})+(\/?)([^\s]+)/g;
var text = 'test google.com/?q=foo basdasd www.url.com/test?q=asdasd#cheese something else';
console.log(text.match(regex));
Expected Result:
["google.com/?q=foo", "www.url.com/test?q=asdasd#cheese"]
If you really want to check for URLs, make sure you include scheme, port, username & password checks just to be safe.
In the context of what you're trying to achieve, you should really put in some delay so that you don't impact browser performance. Regex tests can be expensive when you use complex rules especially so when running the same rule every time a new character is entered. Just think about what you're trying to achieve and whether or not there's a better solution to get there.
With a lookahead:
var exp = /[-\w]+(\.[a-z]{2,})+(\S+)?(\/|\/[\w#!:.?+=&%#!\-\/])?(?= )/g;
I only added this "(?= )" to your regex.
Fiddle

How to validate this string not to contain any special characters in Javascript?

I have a dynamic string like this which may look like:
"69.43.202.97" OR "ngs.pradhi.com"
I want to validate these string contains only numbers, english alphabets and "." . I want to validate this in front end using java script. I thought of using regular expression like this:
function validatePath() {
var path = document.getElementById('server_path').value;
path.match([a-z][0-9]) //or something like this
}
If the path is invalid despite of displaying the alert box I just want to show the error below the text field as soon as the user fills the server path. How can I do that?
My full javascript function looks like this:
function validatePath() {
var path = document.getElementById('server_path').value;
if (path.search(":") == -1){
alert("Invalid server path");
}
else{
var host_name = path.split(":")[0]
if host_name.match("^[a-zA-Z0-9.]*$")) {}
}
}
try path.match("^[a-zA-Z0-9.]*$")
EDIT:
var regex = new RegExp("^[a-zA-Z0-9.]*$");
if(regex.test(host_name)){}
you can use /^[a-zA-Z0-9.]*$/.test(path) which will return true or false
To be different, replace method:
if(path.replace(/^[a-z\d.]*$/i,"")=="")
You can do the check (including port presence) with a regex like this:
^[a-zA-Z0-9\.]+:[0-9]+$
(Note the + instead of * to account for empty path or port (they are not allowed).)
If you are using HTML5, consider the newly introduced "pattern" and "required" attributes, which can posdibly save you some JS code.
See this short demo for an illustration of both technics.
Some links you might find useful:
Regular Expressions
The HTML5 "pattern" attribute
The HTML5 "required" attribute

Categories

Resources