neutrino
neutrino

Reputation: 441

How to with extract url from tweet using Regular Expressions

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

Upvotes: 2

Views: 2595

Answers (2)

Adil
Adil

Reputation: 1

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

Upvotes: 0

Amber
Amber

Reputation: 526613

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].

Upvotes: 3

Related Questions