最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I match everything after @ until space? - Stack Overflow

programmeradmin3浏览0评论

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
  • \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
  • @Xufox Well how can I use Unicode letter in regex? – stack Commented Feb 21, 2016 at 20:27
  • 6 You could use: /@(\S*)/ – Josh Crozier Commented Feb 21, 2016 at 20:28
Add a comment  | 

4 Answers 4

Reset to default 9

You 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/
发布评论

评论列表(0)

  1. 暂无评论