开发者

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 = st开发者_JS百科r.replace(/([^\w])\@([\w\-]+)/gm,'$1<a href="http://twitter.com/$2" target="_blank">@$2</a>'); 


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.


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜