admin管理员组

文章数量:1389754

I'm looking for a way to detect wheter my label object has a "-" character in it.

<label>blabla</label>  -> has not
<label>bla-bla</label> -> has

Im not to familiar with regular expressions.

I'm looking for a way to detect wheter my label object has a "-" character in it.

<label>blabla</label>  -> has not
<label>bla-bla</label> -> has

Im not to familiar with regular expressions.

Share Improve this question edited May 31, 2012 at 16:58 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Apr 1, 2011 at 15:33 MarkusMarkus 4,0388 gold badges50 silver badges65 bronze badges 1
  • 3 If you are trying to find the labels that contain a "-" then you can use the inbuilt contains selector. i.e: var labels = $("label:contains(-)"); – Chandu Commented Apr 1, 2011 at 15:41
Add a ment  | 

1 Answer 1

Reset to default 6

First get the contents of the label, then use the regex /-/.

> "blabla".match(/-/)
> null

> "bla-bla".match(/-/)
> ["-"]

You don't even need a regex here, you can use indexOf:

> "blabla".indexOf('-')
> -1

> "bla-bla".indexOf('-')
> 3

If you just want to select the label element if it contains a "-", you could use:

$('label:contains("-")')

It depends on what you are trying to do.

本文标签: javascriptjquery how to find a single character in htmlStack Overflow