I have this string:
var str = 'این یک @پیا.م تست است';
// I want this ^^^^^
I can select it like this:
/@(.{5})/
But it isn't what I need, because the length of that word which is after @
and before space isn't always
5
. I really don't know why \w
doesn't matches Persian characters. Or even [a-zA-Z]
doesn't work either.
Well, how can I do that?
I have this string:
var str = 'این یک @پیا.م تست است';
// I want this ^^^^^
I can select it like this:
/@(.{5})/
But it isn't what I need, because the length of that word which is after @
and before space isn't always
5
. I really don't know why \w
doesn't matches Persian characters. Or even [a-zA-Z]
doesn't work either.
Well, how can I do that?
Share Improve this question edited Feb 21, 2016 at 20:32 Zakaria Acharki 67.5k15 gold badges78 silver badges105 bronze badges asked Feb 21, 2016 at 20:20 stackstack 10.2k22 gold badges70 silver badges128 bronze badges 3 |4 Answers
Reset to default 9You could use the follwing regex That will return anything beteen @
and fot .
:
@(.*?)[\s]
@ : matches the character @ literally
(.*?) : matches any character (except newline)
\s : match any white space character [\r\n\t\f ]
Hope this helps.
As a Unicocde independent approach you can simply use a negated character class :
'@([^ ]+)'
See demo https://regex101.com/r/oD9hV0/1
The shortest way if you have PCRE (which you do in Javascript) is:
str.match(/@(\S+)/)
That's @-sign followed by at least one non-space. Most of the other character class escapes also have capitalised versions which mean not-this, eg. \D
for not-digit.
Below regex will work
/@(.*?)\s/
\w
is[a-zA-Z0-9_]
,[a-zA-Z]
is for Latin letters, because Persian letters aren’t included in the Latin Unicode range. – Sebastian Simon Commented Feb 21, 2016 at 20:26/@(\S*)/
– Josh Crozier Commented Feb 21, 2016 at 20:28