locoboy
locoboy

Reputation: 38940

Parsing twitter @name with regex and javascript

I'm trying to parse twitter name tags using javascript and was wondering if this regex would do the trick. I think most of this works, but am just wondering if I'm using the $1 and $2 properly. Can people confirm that this is right and if so, generally explain what the $1 and $2 represent?

str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1<a href="http://twitter.com/$2" target="_blank">@$2</a>'); 

Upvotes: 1

Views: 2245

Answers (2)

mu is too short
mu is too short

Reputation: 434665

I think you're using the $n right:

$n or $nn
Where n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.

So your $1 will be replaced with what matched [^\w] and $2 will be replaced with what matched [\w\-]+. However, I think you want a bit more in your first group so that you can properly match strings like "@pancakes", a (^|\W+) would serve you better:

str = str.replace(/(^|\W+)\@([\w\-]+)/gm,'$1<a href="http://twitter.com/$2" target="_blank">@$2</a>');

You might want to read up on JavaScript regular expressions.

And, thanks to Kobi, you could use a simpler regular expression but you'll have to change change your replacements a little bit:

str = str.replace(/\B@([\w-]+)/gm, '<a href="http://twitter.com/$1" target="_blank">@$1</a>');

And you don't need to escape the hyphen when it can't be mistaken for a range indicator.

Upvotes: 5

Ryan Olds
Ryan Olds

Reputation: 4847

The first group, ([^\w]), needs to be optional, so try this: /([^\w])?\@([\w-]+)/gm

A great online tool for testing a regex can be found here: http://gskinner.com/RegExr/

Upvotes: 2

Related Questions