EasyBB
EasyBB

Reputation: 6544

Regex to extract word from a line

I am trying to extract the word(s) inside round brackets.

Code:

$('.vote').each(function() {
 var vote_count = $('.vote .likedButton').attr('title');
 var split_count = vote_count.replace(/\(([^)]+)\)/,"");
 alert(split_count);
});

The string I am trying to get-

Message reputation : 50% (2 votes) trying to get 2 votes

The JavaScript Regex I have above is only deleting this line. Am I missing some regex or do I need to use

.match(/\(([^)]+)\)/);

Can someone explain thank you

Upvotes: 1

Views: 93

Answers (1)

Roko C. Buljan
Roko C. Buljan

Reputation: 205987

LIVE DEMO

$('.vote').each(function() {

   var str = $('.likedButton', this).attr('title');
   $(this).prepend( str.match(/\(([^)]+)\)/)[1] );

});

Upvotes: 1

Related Questions