edward edward - 4 months ago 8
Javascript Question

I need to get a certian part out of a string using Jquery

I need to get the

jsmith
part out of the following elements alt attribute

<img src="http://www.test.com" class="img-thumbnail" alt="John Smith (jsmith)">


So I'm running this

$('.img-thumbnail').attr('alt');


which gives me

"John Smith (jsmith)"


I need to get the 'jsmith' part out

any ideas?

Answer

Try this

$('.img-thumbnail').attr('alt').match(/\(([^)]+)\)/)[1]

Please note that this will only work if you want to get the text inside brackets. FIDDLE. Hope this helps

Comments