How can I check for the presence of @ in a regex?
I know that a character @
is an operator in regex, but I need to check if it is inserted in a word, then I need to check if it is present @
.
How can I write my own regex?
Actually my reg-exp (in Javascript):
/^[a-zA-Z0-9_]+$/
I need to provide the ability to insert ti @
too.
Thank you for your help.
source to share
You don't need to hide the symbol @
. It is not a regular expression metacharacter.
/^\w*@\w*$/
This ensures that the string must contain a character @
.
OR
Use a positive result in the beginning.
/^(?=.*@)[\w@]+$/
A positive lookahead states that the string to be matched must contain a character @
.
> /^(?=.*@)[\w@]+$/.test('foo@bar')
true
> /^(?=.*@)[\w@]+$/.test('foo909bar')
false
source to share