This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
I have this regulular expression to validate email id but I can't allow two consecutive hyphen on domain, However it doesn't works for me whatever the fix I made. Could anyone please help?
/^[a-zA-Z\-0-9](([^<>()[\]\\.,;:\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,}))$/
Test Id to fail case: sant#y--t.com
Please help, Thanks for your assisstance!
^[a-zA-Z\-0-9](([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\-\].,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z0-9](?:[a-zA-Z0-9]|-(?!-))*\.)+[a-zA-Z]{2,}))$
Demo
Change:
([a-zA-Z\-0-9]+\.)
To:
([a-zA-Z0-9](?:[a-zA-Z0-9]|-(?!-))*\.)
Match one [a-zA-Z0-9], and then loop through [a-zA-Z0-9] or - (as long as it isn't followed by another -) repeated 0+ times.
Related
This question already has answers here:
Regular expression to match a dot
(7 answers)
Closed 2 years ago.
Using regex, I'm trying to check whether there's only 2-4 characters used after a .. At the moment, i got it working to detect when its less than 2 characters but after 4 characters, it still deems it as successful. How can I fix this? This is what I have written down:
/.[a-zA-Z]{2,4}$/
You need to escape the dot (.).
/\.[a-zA-Z]{2,4}$/
. is the control character in RegExp, you should to escape it:
/\.[a-zA-Z]{2,4}$/
And, to ignore case, add the i flag:
/\.[a-z]{2,4}$/i
This question already has answers here:
Regex remove special characters
(4 answers)
Closed 2 years ago.
I am trying to strip all non numbers and this symbol * from a string.
An example would be
Input: "gdfgdf234dg54gf*23oP42"
Output: "23454*2342"
I have the following which removes everything apart from numbers. The issue is that it also removes the *. I am unsure how to ignore one character.
string.replace(/\D/g,'');
I am sure I will get ridiculed on here saying what else have I tried, but have searched for numerous answers on here.
Can anyone help?
Try [^\d*]
Regex Demo
console.log("gdfgdf234dg54gf*23oP42".replace(/[^\d*]/g,''))
This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 3 years ago.
I have following regex string to check for valid email formats
/^(([^<>()[\]\\.,;:\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,}))$/
At the very end of it I want to add unicode flag u so it will look like this
/^(([^<>()[\]\\.,;:\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,}))$/u
However I am getting error saying that regex becomes invalid with unicode flag. Is there any possibility to set it here?
There are multiple solutions in order to validate unicode characters, but this flag cannot be used like that. The \u flag is most used to be followed by a char code like \u00C0.
I think the most reliable solution is to specify the range of accepted unicode characters in the regex.
Something like this should work:
/^(?!\.)((?!.*\.{2})[a-zA-Z0-9\u00E0-\u00FC.!#$%&'*+-/=?^_`{|}~\-\d]+)#(?!\.)([a-zA-Z0-9\u00E0-\u00FC\-\.\d]+)((\.([a-zA-Z]){2,63})+)$/
The solution applied here is to support characters from à to ü.
Regex tester: https://www.regexpal.com/?fam=108260
Related question for mathching unicode characters: Matching accented characters with Javascript regexes
This question already has answers here:
Validate phone number with JavaScript
(30 answers)
Closed 5 years ago.
console.log(/\d+?\d+?\d+?-\d+?\d+?\d+?-\d+?\d+?\d+?\d+?$/.test("555-555-55539"));
Answer --> true
I was looking for false, i am validating phone numbers. e.g. 555-555-5555 is a correct response([0-9])
I am a newbie to regex, can anyone explain what i am doing wrong here?
How about this.
console.log(/\d{3}-\d{3}-\d{4}$/.test("555-555-55539"));
You used wrong quantifiers in your regex. You made them lazy (+?), but it will still match all characters until the next character from regex is found. In case of your last quantifier (just before $) it will match all digits until the end of string is found. Hence it matches not only one digit but all of them. Same thing happens before each hyphen (555555555-5555-555555555 is valid for your regex).
This question already has answers here:
RegEx for Javascript to allow only alphanumeric
(22 answers)
Closed 8 years ago.
I am trying to interrupt a form submission to test field for alphanumeric characters. After googling i found a lot of this implementation for regex that everyone claims works great...
if( !jQuery('input[name=myUsername]').val().test(/^[a-zA-Z0-9]+/) ) {
alert('ERROR: Your username contains invalid characters. Please use numbers and letters only. No spaces, no punctuation.');
jQuery('input[name=myUsername]').focus();
return false;
}
However, this ONLY returns false and creates an alert if the value STARTS with a non-alphanumeric character. If i enter "bo$$" it allows it as alphanumeric even tho $ is not an alphanumeric character... If i enter "$$ob" it fails and alerts.
How do i fix this so that it will fail for ANY invalid character in the entire value? I've tried .match() instead of .test() but same issue im assuming it's something in the regex that is wrong
^[a-zA-Z0-9]+$
put $ the end anchor to limit that.Without $ your regex will make a partial match.
$ assert position at end of a line
bo$$ will make a partial match upto bo as $ is not there.
Use anchor $ to avoid matching unwanted character at the end:
/^[a-zA-Z0-9]+$/