Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression starts with @ symbol

In regular expression, how do I look for matches that start with an @symbol? The symbol cannot be in the middle of a word (link in an email address).

For example, a string that looks like this:

@someone's email is [email protected] and @someoneelse wants to send an email.

The expression I'd use is /^@[\w]/g

It should return:

@someone's

@someoneelse

The expression I use doesn't seem to work.

like image 870
arjay07 Avatar asked Oct 28 '25 02:10

arjay07


1 Answers

You can utilize \B which is a non-word boundary and is the negated version of \b.

var s = "@someone's email is [email protected] and @someoneelse wants to send an email.",
    r = s.match(/\B@\S+/g);

console.log(r); //=> [ '@someone\'s', '@someoneelse' ]
like image 152
hwnd Avatar answered Oct 29 '25 19:10

hwnd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!