Regular expression for domain validation in nodejs - javascript

Hi I want to validate if only a domain is present in a string. For example the string "https://anyDomain" or "https://anyDomain/" should return true. Any other strings like "https://anyDomain/in" should return false.
The regex code that I have currently returns true for "https://anyDomain" but not for "https://anyDomain/". Below is the code:
var url = "https://anyDomain/";
var reg = /^https:\/\/[^\/]*$/;
console.log(reg.test(url));

Try the following regex:
^https:\/\/.*.com(\/)?

Related

Nodejs regexp validation for sip URI string

var uriRegExp = /^(sip):\(?([0-9]{3})\)?[- ]?([0-9]{3})[- ]?([0-9]{4})#\w+(\ w+)*(\.\w)(\.\w{2,3})+$/;
Is this a correct regular expression for validating the string
sip:1-999-123-4567#voip-provider.example.net ?
No, this regex does not match your string.
If you want to know why you could have a look at https://regex101.com/r/EC0xFN/1 .
There you can interactively build and check your regex with different input strings.
Here is a simple sip URI validator, that i have created, using regular expression.
function myFunction() {
var str = "sip:+91989556926#test.est.test.com";
var regExp = /^(sip):(\S+[0-9])#\S+(\w+([.-]?\w+)*).(\w{2,3})$/;
var result = regExp.test(str);
document.getElementById("demo").innerHTML = result;
}
Please check the link.
[https://regex101.com/r/5vMfI9/4][1]

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.

Regular Expression wrong match

I am trying a regex to validate an Url.
var url = "https://www,google.com";
var urlRegex = /(https|http)?:\/\/(?:\w[\-\w.]+)(?:\/[\-\w+&##\/%=~_|!:,.;]*)?(?:\?[\-A-Z0-9+&##\/%=~_|!:,.;]*)?/i;
var result = urlRegex.test(url);
so i am getting "result" as true but i should get false as "url" contains comma.
Kindly help.
Add anchors (^ for beginning of a string, and $ for the end of the string):
^(https|http)?:\/\/(?:\w[\-\w.]+)(?:\/[\-\w+&##\/%=~_|!:,.;]*)?(?:\?[\-A-Z0-9+&##\/%=~_|!:,.;]*)?$
^ ^
See demo
You are getting true since your regex partly matches.
Use ^(https|http)?:\/\/(?:\w[\-\w.]+)(?:\/[\-\w+&##\/%=~_|!:,.;]*)?(?:\?[\-A-Z0-9+&##\/%=~_|!:,.;]*)?$
instead.

Regular expression failed in URL address test

I try to build an Regular expression to check valid URL address. for now I tested different address and all was good , but those next (valid) address's failed:
url = "http://example.com/tr/vvf/index.php/docs/po/trf"
//url = "http://example-a.mydomain.com/test/ny" also not working
var pattern = new RegExp("(https|ftp|http)://[\w-]+(\.[\w-]+)+([\w.,#?^=%&:/~+#-]*[\w#?^=%&/~+#-])?");
pattern.test(url)
I think because of the index.php/doc... Any ideas how to fix it
Just use regex literal instead of RegExp object:
var pattern = /(https|ftp|http):\/\/[\w-]+(\.[\w-]+)+([\w.,#?^=%&:\/~+#-]*[\w#?^=%&\/~+#-])?/;
RegExp works with a string, that requires you to do double escaping so \w becomes \\w in it.
See it working here

Extracting a substring with a JavaScript regular expression;

Consider this code:
var myregexp = "\\*(.+)"; // set from another subsystem, that's why I'm not using a literal regexp
var input = "Paypal *Steam Games";
var output = input.match(new RegExp(myregexp, 'gi'), "$1");
The output is ["*Steam Games"], but I would like it to be just ["Steam Games"].
What is wrong?
PS A great resource I found today: http://regex101.com/#javascript
match doesn’t accept a second argument.
Since you have the global flag set (and I assume it’s intentional), you’ll need exec to find all of the first groups:
var m;
while ((m = re.exec(input)) {
alert(m[1]); // Get group 1
}
var str = "Paypal *Steam Games";
var reg = /\w+\s?\*(\w+\s?\w+)/; // or your exp will work too `/\*(.+)/;`
console.log(reg.exec(str)[1]); // result Steam Games
JSFiddle
You'll get Steam Games from your string with help of /\w+\s?\*(\w+\s?\w+)/ exp
In JavaScript there are three main RegExp functions:
exec A RegExp method that executes a search for a match in a
string. It returns an array of information.
match A String method that executes a search for a match in a
string. It returns an array of information or null on a mismatch.
test A RegExp method that tests for a match in a string. It
returns true or false.

Categories

Resources