开发者

jquery: how to find a single character in html

开发者 https://www.devze.com 2023-02-21 02:56 出处:网络
I\'m looking for a way to det开发者_开发技巧ect wheter my label object has a \"-\" character in it.

I'm looking for a way to det开发者_开发技巧ect 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号