Regex to validate an email address [duplicate] - javascript

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.

Related

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());
}

Getting a true or false reply to a Regex match? [duplicate]

This question already has answers here:
Return true/false for a matched/not matched regex
(5 answers)
Closed 3 years ago.
I'm trying to match an entire string against a regex formula. This is for validating if a phone number field is likely correct (just based on allowed characters, anyone can make up an number). I've played with Regex before but never truly understood the nuances that make it powerful.
Below I have my dummy phone number and I have the regex I'm using. As you can see I'm simply comparing the length of the match vs the length of the string and if they match the number must be valid.
Is there a way to get a simple true/false reply from a Regex check on an entire string?
var num = '+1 (888) 456-7896';
var regex = /[0-9+ ()-]*$/;
var found = num.match(regex);
console.log(found[0].length);
console.log(num.length);
You can use test()
var found = regex.test(num);

regex match number in parentheses (Firefox) [duplicate]

This question already has answers here:
Javascript regex (negative) lookbehind not working in firefox
(4 answers)
Closed 4 years ago.
I have string like this:
(3) Request Inbox
Now I want to detect number 3 in parentheses. Pay attention just 3. I write this regex in javascript but it doesn't work in Firefox.
var regex = new RegExp(/(?<=\()\d+(?:\.\d+)?(?=\))/g);
Error in console: SyntaxError: invalid regexp group
Demo link
Positive lookbehind is not supported by most browsers so use an alternative way to get your answer.
Something like this,
var string = "somestring(12)";
var exp = /(?:\()(\d+)(?:\.\d+)?(?=\))/;
var mat = string.match(exp);
if(mat) {
console.log(mat[1]);// second index
}
This should only give 12 as the answer
For a full match try this:
var regex = new RegExp(/(?=\d+\))\d+/g);

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

Javascript regex in attribute [duplicate]

This question already has answers here:
Create RegExps on the fly using string variables
(6 answers)
Closed 8 years ago.
I need to use data attribute for regex just as
<div data-regex="REGEX-HERE">
and then get the value by javascript and put in a variable. and then do a test like
var regex = $(this).attr("data-regex");
regex.test(name)
when I tried to use "^[\x20-\x7E]+$" for testing english character is didn't work.
Note when I tried this
var regex = /^[\x20-\x7E]+$/;
It worked.
Thanks in advance
You can do this:
var regex = new RegExp("^[\x20-\x7E]+$",""); // Modifiers on the tend
So finally:
var regex = new RegExp($(this).data("regex"));
regex.test(name)

Categories

Resources