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
Related
This question already has an answer here:
Allow alphanumeric with spanish regex in javascript?
(1 answer)
Closed 1 year ago.
I have the following regex set up to accept words and some special characters:
const regex = /^[\w\-'.,?\/()\[\]!&\s]+$/;
I want to extend this to also include the range of special characters in Spanish: ñáéíóú
I found this answer which provides a regex for all special chars, but I'm not sure how to incorporate this kind of solution into my already existing regex.
You can simply add those characters to the class you already have in your regex:
const regex = /^[\wñáéíóú\-'.,?\/()\[\]!&\s]+$/;
It is not needed to add the u modifier.
NB: it is not really necessary to escape the [ character inside a character class.
This question already has answers here:
Learning Regular Expressions [closed]
(1 answer)
Match empty string, comma, hyphen or underscore once using regex
(2 answers)
Closed 4 years ago.
I have a requirement to do a field validation using regEx. The requirement is not to have any special characters. It is working for me for a single white space, but not for multiple white spaces. It does not work if there is a white space at the beginning. Can you please help me with this?
This is what I used:
^(\w+ ?)*$
You might be looking for
^[\w ]*$
The ^ and $ are anchors for the start/end of the string, the [...] is called a character class and would allow only [A-Za-z0-9_ ]. The * is a quantifier and means zero or more times, thus the expression would also allow an empty string. If this is not what you want, change it to + instead of the *. Please note that this would also allow a string with only spaces (it really depends on what you want).
This question already has answers here:
javascript regex - look behind alternative?
(8 answers)
Closed 6 years ago.
I'm converting a python script I wrote to javascript. My python script has the following regex to match single instances of '\':
re.sub(r'(?<!\\)(\\{1})(?!\\)', r'\\', word)
I got a compiler error when trying to run this in js:
"Invalid regular expression: /(?<!\\)(\\{1})(?!\\)/: Invalid group"
After some searching found out that regex in js does not support look behinds.
I looked at this answer and they used:
^(?!filename).+\.js
in the form of a negative look-ahead from the start of the string, which does not help me as I need to change '\' to '\\' anywhere in the string.
I do not think this is a duplicate question as my question is trying to determine how to avoid and match the same character at different points in a string, while the linked question seeks to avoid a specific phrase from being matched.
I need to match '\' characters that do not have '\' either before or after them.
You always can use capture groups instead of lookbehind
string.match(/(^|[^\\])(\\{1})(?!\\)/)[2]
let replaced = "a\\b\\\\".replace(/(^|[^\\])(\\{1})(?!\\)/, x => x[0] == '\\' ? x : 'value')
console.log(replaced)
will return you same thing as (?<!\\)(\\{1})(?!\\)
Just match without assertions (^|[^\\])\\([^\\]|$) then substitute them back.
Note that this will tell you nothing about if it is escaping anything or not.
That regex is more complex.
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]+$/
This question already has answers here:
Regular expression to match non-ASCII characters?
(8 answers)
Closed 8 years ago.
Lets say I have a regexp that looks like:
\w+
Then this string would pass:
helloworld
However this won't:
héllowörld
It will stop at é (and theöwill break it as well) even though for a human héllowörld doesn't sound so far fetched as a single word.
Is there a way I can improve \w so it will also include special word characters? Or do I have to append every special latin character into my regexp like this into:
[\wéèåöä...........]+
Because that doesn't seem like the best option to try and figure out what all the different special latin characters there are in the world that would be reasonable.
What options do I have?
\w match any word character [a-zA-Z0-9_]. It doesn't match non-english character.
Read this post for Regular expression to match non-english characters?
Sometimes I use an inverse method to match non-english among the other characters. Check this out
var string = "你好 κόσμος привет šđčߣłćž çë asgfgrtzj 657 #$%&/()=?*!";
The pattern below
var pattern = /([^0-9]+)/gi;
will exclude all numbers
你好 κόσμος привет šđčߣłćž çë asgfgrtzj #$%&/()=?*!";
adding special characters from the above to the pattern
var pattern = /([^0-9#$%&/()=?*!]+)/gi;
the final string would look as following
你好 κόσμος привет šđčߣłćž çë asgfgrtzj