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?
You can match a space character with just the space character; [^ ] matches anything but a space character.
What is used to match anything except a whitespace? The complement, \S , matches any non-whitespace character.
If you want . to match really everything, including newlines, you need to enable "dot-matches-all" mode in your regex engine of choice (for example, add re. DOTALL flag in Python, or /s in PCRE.
\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.
As a Unicocde independent approach you can simply use a negated character class :
'@([^ ]+)'
See demo https://regex101.com/r/oD9hV0/1
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With