javascript - Get class with regex - Stack Overflow

<span class="here one-two-sdfs test">click1<span> <br ><span class=&q

<span class="here one-two-sdfs test">click1</span> <br />
<span class="here one-two-3er te3st">click2</span> <br />
<span class="here one-two-sdwrer test">click3</span> <br />
<span class="here one-two-s-ere test">click4</span> <br />
<span class="here one-two-wer-r test">click5</span> <br />
<span class="here one-two test">click6</span> <br />

$('.here').click(function(){
    var one-two = ????
    alert(one-two);
    })​

​ fiddle

i would like -

if i click on click1 then alert show me one-two-sdfs

if i click on click2 then alert show me one-two-3er

if i click on click3 then alert show me one-two-sdwrer

if i click on click4 then alert show me one-two-s-ere

if i click on click5 then alert show me one-two-wer-r

if i click on click6 then alert show me one-two

i would like show class with begin one-two- how can i make it?:)

<span class="here one-two-sdfs test">click1</span> <br />
<span class="here one-two-3er te3st">click2</span> <br />
<span class="here one-two-sdwrer test">click3</span> <br />
<span class="here one-two-s-ere test">click4</span> <br />
<span class="here one-two-wer-r test">click5</span> <br />
<span class="here one-two test">click6</span> <br />

$('.here').click(function(){
    var one-two = ????
    alert(one-two);
    })​

​ fiddle

i would like -

if i click on click1 then alert show me one-two-sdfs

if i click on click2 then alert show me one-two-3er

if i click on click3 then alert show me one-two-sdwrer

if i click on click4 then alert show me one-two-s-ere

if i click on click5 then alert show me one-two-wer-r

if i click on click6 then alert show me one-two

i would like show class with begin one-two- how can i make it?:)

Share Improve this question edited Jul 25, 2012 at 9:38 Luck Mendizo asked Jul 25, 2012 at 9:32 Luck MendizoLuck Mendizo 211 silver badge4 bronze badges 1
  • 2 Did you try doing any reading on regular expressions and work yourself? – T.J. Crowder Commented Jul 25, 2012 at 9:39
Add a ment  | 

3 Answers 3

Reset to default 4

http://jsfiddle/tMzAH/10/

$('.here').click(function(){
    var onetwo = this.className.match(/\bone-two[^\s]*/);
    console.log(onetwo[0]);
})

This should work.

$('.here').click(function(){
    var one_two = $(this).attr("class").match(/\bone-two\S*/)[0];
    alert(one_two);
    })​

Get the className of the element you clicked. And then split it. You do not need RegEx for this ;)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745671362a4639422.html

相关推荐

  • javascript - Get class with regex - Stack Overflow

    <span class="here one-two-sdfs test">click1<span> <br ><span class=&q

    21天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信