How can I match everything after @ to place?

I have this line:

var str = 'این یک @پیا.م تست است'; // I want this ^^^^^ 

I can select it like this:

 /@(.{5})/ 

But this is not what I need, because the length of this word, which is after @ , but before the space not always 5 . I really don't know why \w does not match Persian characters. Or even [a-zA-Z] doesn't work either.

Well, how can I do this?

+6
source share
4 answers

You can use regex follwing, which will return anything, @ and fot . :

@ (. *?) [\ s]

@ : matches the @ character literally

(. *?) : matches any character (except a newline)

\ s : match any space character [\ r \ n \ t \ f]

Hope this helps.

+3
source

As an independent Unicocde approach, you can simply use a negative character class:

 '@([^ ]+)' 

See demo https://regex101.com/r/oD9hV0/1

+2
source

The shortest way if you have PCRE (what you do in Javascript):

 str.match(/@(\S+)/) 

This is an @ sign followed by at least one non-space. Most other character class screens also have uppercase versions that do not mean this, for example. \D for digits.

+2
source

Below regex will work

 /@(.*?)\s/ 
+1
source

All Articles