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.

+3


source to share


2 answers


^[a-zA-Z0-9_@]+$

      

or



^[\w@]+$

      

This will allow you to insert a character here @

as it is inside the charracter class.

0


source


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

      

DEMO

0


source







All Articles