I am trying to create a regular expression for the string filtering. I want to get the symbol "@" and anything that is written after that and before a space.
Can someone help me with this?
For example:
hi I am @vaibhav .
The expected result this regular expression should give is vaibhav
.
I made this:
/@[a-z]*/
However, I am not sure if this will confirm to the above mentioned criteria.
To get a substring from the @
up to the first space after it, use
@\S+
See demo
The \\S
means a non-whitespace character .
If you do not need @
, use a capturing group:
@(\S+)
The value you need will be in Group 1. See another demo .
If you are using JavaScript:
var re = /@(\\S+)/g; var str = 'hi I am @vaibhav . hi, and I am @strib .'; var m; while ((m = re.exec(str)) !== null) { document.write("The value is: <b>" + m[1] + "</b><br/>"); }
The simplest solution is to use a negated set.
'@'
'@'
' '
If you're trying to match and capture you can accomplish that like this:
[^@]*@([^ ]*).*
[ Live Example ]
If you only want to search then you don't need to match the whole string and you can just extract the actual match section:
@([^ ]*)
[ Live Example ]
The most complicated situation is where you need to deal with an escaped '@'
. Here's an example of a match using that:
(?:[^\\@]|\\.)*@([^ ]*).*
[ Live Example ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.