2

Ok so i'm executing the following line of code in javascript

RegExp('(http:\/\/t.co\/)[a-zA-Z0-9\-\.]{8}').exec(tcont);

where tcont is equal to some string like 'Test tweet to http://t.co/GXmaUyNL' (the content of a tweet obtained by jquery).

However it is returning, in the case above for example, 'http://t.co/GXmaUyNL,http://t.co/'.

This is frustracting because I want the url without the bit on the end - after and including the comma.

Any ideas why this is appearing? Thanks

Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
neutrino
  • 341
  • 1
  • 3
  • 19
  • possible duplicate of [regex to find url in a text](http://stackoverflow.com/questions/5461702/regex-to-find-url-in-a-text) – Justin Pihony Mar 24 '12 at 17:30

2 Answers2

3

First, get rid of the parens in the pattern - they're unnecessary:

RegExp('http:\/\/t.co\/[a-zA-Z0-9\-\.]{8}').exec(tcont);

Second, a regex match returns an array of matching groups - you want the first item in it (the entire match):

var match = RegExp('http:\/\/t.co\/[a-zA-Z0-9\-\.]{8}').exec(tcont);
if(match) {
    var result = match[0];
}

The reason you had "a part on the end" is because your result is actually an array - the parens you had in the expression were resulting in an extra matching group (the portion they were around), which would be match[1].

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Excellent, that worked thank you so much - I'm not all that experienced with regex so that really helps. – neutrino Mar 24 '12 at 17:32
  • 1
    I think the `.` should be escaped. – pimvdb Mar 24 '12 at 17:34
  • @pimvdb technically, yes, but since all URLs on Twitter get transformed into `t.co` URLs, it doesn't actually matter in this case. – Amber Mar 24 '12 at 17:41
  • @Amber: Very true, I was just nitpicking :) – pimvdb Mar 24 '12 at 17:43
  • pimvdb is absolutely correct: the `.` should be escaped. In a regular expression, `.` matches a single character. In this particular example, it will of course match a literal `.` in the source. However, the regex would also match `http://tico/GXmaUyNL` (for example). – Mike Dimmick Mar 18 '13 at 12:01
0

Try this : RegExp('http:\/\/t\.co\/[a-zA-Z0-9\-\.]{8}').exec(tcont);

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
Adil
  • 1