Careless Careless - 1 year ago 65
Javascript Question

Find word next to some symbol

If I have a

and I type
#SomeName is very happy today! #AnotherName
, I want to make
var name = #SomeName
. I want to store in the
var only word that is next to
. Can someone tell me how to do it in jQuery?

Answer Source

Given that there could be an unlimited number of # values in the string within the textarea I would suggest you create an array to contain them all instead of a single string. With that in mind you can use a Regular Expression and the match() method. Try this:

var matches = textareaValue.match(/(^|\s)(#[a-z\d-]+)/ig);

Working example

The first part of the Regex is a negative lookahead to ensure the # is preceded by a space. The second part is retrieving the actual value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download