Philip Doorger
Philip Doorger

Reputation: 1

How to get text from attribute with for example regular expression?

   1. <a class="click" data-test="CodeMirror-sizer('@aaa', 1)-er">
   2. <a class="click" data-test="CodeMirror-sizer44('@bbb', 2)-fgd">
   3. <a class="click" data-test="CodeMirror-sizer366('@ccc', 2)-gr4f">
   4. <a class="click" data-test="CodeMirror-sizer33('@ddd', 1)-erw">

$(".click").click(function(){
    var text = ???;
   alert(text);
})

How to get text from attribute with for example regular expression? I would receive:

from 1 = @aaa

from 2 = @bbb

from 3 = @ccc

from 4 = @ddd

LIVE jsFIDDLE

Upvotes: 0

Views: 88

Answers (4)

sangram parmar
sangram parmar

Reputation: 8726

try this

http://jsfiddle.net/Xr7bw/5/

$(".click").click(function(){
    var tag = $(this);
   alert(tag.attr('data-test').split('\'')[1]);
})

Upvotes: 0

C&#233;ryl Wiltink
C&#233;ryl Wiltink

Reputation: 1909

$(".click").click(function(){
    var pattern = /@[a-z]{3}/i
    var text = $(this).attr('data-test')

    alert(text.match(pattern ));
})

See the fiddle here: http://jsfiddle.net/Xr7bw/4/

But it all depends on the pattern, this one just matches and "@" with three characters behind it in the range [a-z]... If you can be more precise with what possible values will be, we can be more precies with capturing them

Upvotes: 2

Alex K.
Alex K.

Reputation: 175766

One way:

var text = $(this).data("test");
alert ( text.match(/^.*\('(.*)\s*'/)[1] );

Upvotes: 0

colestrode
colestrode

Reputation: 10658

You could just split on the single quote character and take the second value returned:

$(".click").click(function(){
    var text = $(this).data('test').split("'")[1];
    alert(text);
});

Working Demo

Upvotes: 4

Related Questions