Jared Jared - 4 months ago 10
jQuery Question

How do I select string after a specific symbol in javascript/Jquery?

Let says I have a string that says

and the cow went @moo
, and I want to only select moo... how would I go about that?

Answer
var moo = "blah blah cow @moo".match(/@([^ ]*)/)[1];

The regular expression @([^ ]*) means: find the character @, then get all the characters afterward until a space is encountered (or implicitly the end of the string). Any part of the regex enclosed in parenthesis is stored and returned in an array. The first element is always the entire match, in this case @moo, afterwards the match inside each parenthesis in the regex from left to right.

Comments