Reputation: 1
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
Upvotes: 0
Views: 88
Reputation: 8726
try this
$(".click").click(function(){
var tag = $(this);
alert(tag.attr('data-test').split('\'')[1]);
})
Upvotes: 0
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
Reputation: 175766
One way:
var text = $(this).data("test");
alert ( text.match(/^.*\('(.*)\s*'/)[1] );
Upvotes: 0
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);
});
Upvotes: 4