Need assistance with regex in javascript [duplicate] - javascript

This question already has answers here:
What is a regular expression which will match a valid domain name without a subdomain?
(23 answers)
Closed 6 years ago.
I would like to validate url's in javascript before user proceeds further.
The urls below should match;
http://google.com
http://www.google.com
www.google.com
google.com
And not match;
http://google
http://www.google
www.google
google
Please help me im really bad at regex

try like this
var urlR = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)
(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var url= content.match(urlR);
or
var regex = new RegExp("^(http[s]?:\\/\\/(www\\.)?|ftp:\\/\\/(www\\.)?|www\\.){1}([0-9A-Za-z-\\.#:%_\+~#=]+)+((\\.[a-zA-Z]{2,3})+)(/(.)*)?(\\?(.)*)?");
if(regex.test("http://google.com")){
alert("Successful match");
}else{
alert("No match");
}

Related

JS URL regex, allow only urls and empty string [duplicate]

This question already has answers here:
Regular expression which matches a pattern, or is an empty string
(5 answers)
Closed 2 years ago.
I have the following regext:
var regex = /^(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)/g;
function test() {
alert(regex.test(document.getElementById("myinput").value));
}
I want to allow url or empty string. regex solution please
How do I allow empty in this case?
https://jsfiddle.net/6kptovwc/2/
Thanks
Add an alternation with empty string (I've simplified a bit your regex):
^((?:https?:\/\/)?(?:www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b[-a-zA-Z0-9#:%_\+.~#?&\/=]*|)$
or
^((?:https?:\/\/)?(?:www\.)?[-\w#:%.+~#=]{2,256}\.[a-z]{2,6}\b[-\w#:%+.~#?&\/=]*|)$
Demo & explanation
Just add OR(||) condition
function test() {
const elm = document.getElementById("myinput")
alert(elm.value === '' || regex.test(elm.value));
}
^$|pattern
var regex = /^$|(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)/g;

Validate an email address always fails [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 3 years ago.
Please see the code below:
function (string regex, string value)
{
var regularExpression = new RegExp(regex);
return regularExpression.test(value);
}
Why does it fail? It seems to fail for everything I enter. I got the code from here: How to validate an email address in JavaScript? i.e. the community answer that starts: "I've slightly modified Jaymon's answer".
Try this
function validateEmail(email) {
var re = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}

Python Regex to get string(extension) after special character [duplicate]

This question already has an answer here:
Extract until end of line after a special character: Python
(1 answer)
Closed 5 years ago.
I have a string want to get string after special character which is extension.
i tried and it is works fine in javascript but wasnt in python.. how to do that?
here is my JS snippet,
my_str_0 = ".exr[6,7]";
my_str_1 = "/home/mohideen/test_dir/Samp_8860-fg_paint_%04d.exr[6] 1-7";
my_str_2 = "/home/mohideen/test_dir/Samp_8860-fg_paint_%05d.png[1] 1-10";
my_str_3 = "/home/mohideen/test_dir/Samp_8860-fg_paint_%05d.jpg";
for (var i in [my_str_0, my_str_1, my_str_2, my_str_3]) {
var reg = /([^.]*)$/.exec(eval("my_str_"+i));
console.log(reg[0]);
}
here is my python regex link
Use MultiLine Flag.
See here in this link

Why the name validation is invalid? [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Amend Regular Expression to allow German umlauts, French accents and other valid European letters
(4 answers)
Closed 6 years ago.
Why the name is not valid when user is type:
André
René
François
Anne-Marie
CODE:
var myNameFilter = /^([a-zA-Z ]+)$/;
var a = $('#firstname').val();
if(a=='') {
alert("You stupid your name is empty. Fill it in NOW!!!");
return false;
} else if (!myNameFilter.test(a)) {
alert("Are you stupid? how can you type your name wrong. you idiot. fix it, dont call me for this.");
return false;
}
[a-zA-Z] detect only english. they contain unicode characters and so it will not detect it.
Now check it
var a="André,René,Fran çois,Anne-Marie,éç,é-çé ç,Éric,Hélène".split(",");
var myNameFilter = /^([a-zA-Z\-éçèàùâêîôûëïüÿçÉ\s]+)$/;
i=0;
while(i<a.length){
console.log(a[i]+"->"+myNameFilter.test(a[i]));
i++;
}

Regex to validate an email address [duplicate]

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 8 years ago.
I am not expert in JavaScript and need to get this regex to work:
function validateEmail(email) {
var re = /[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,22}/;
return re.test(email);
}
Currently this doesn't work fine, even for myemail#hotmail.com.
I don't need a new regex, just few changes to this one to get it to work.
You need to use the case-insensitive flag, i:
var re = /[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,22}/i;
Without this, it would only match upper-case Latin letters, e.g. MYEMAIL#HOTMAIL.COM.
See MDN for a list of supported flags.

Categories

Resources