最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get class with regex - Stack Overflow

programmeradmin1浏览0评论
<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 ;)

发布评论

评论列表(0)

  1. 暂无评论