jQuery:检查字符是否在字符串中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3139477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: Check if character is in string
提问by Jonathan
I want the simplest way to check if an underscore (_) is in a variable using jQuery and do something if is not..
我想要最简单的方法来检查下划线 (_) 是否在使用 jQuery 的变量中,如果不是,则执行某些操作。
if ( '_ is not in var') {
// Do
}
Thanks!
谢谢!
回答by Reigel
回答by Nishad Up
There is some more ways to do this.
还有一些方法可以做到这一点。
indexOf() method.
if( str.indexOf('_') != -1 ){ //do something } else{ //do something }
Search() method.
if( str.search("_")!-1 ){ //do something } else { //Do something }
:contains() selector
if( $("p:contains(_)") ).length{ //Do something } else{ //Do something }
with regular expression
if( str.match(/_/g) ).length{ //Do something } else{ //Do something }
indexOf() 方法。
if( str.indexOf('_') != -1 ){ //do something } else{ //do something }
搜索()方法。
if( str.search("_")!-1 ){ //do something } else { //Do something }
:contains() 选择器
if( $("p:contains(_)") ).length{ //Do something } else{ //Do something }
使用正则表达式
if( str.match(/_/g) ).length{ //Do something } else{ //Do something }
I Think the simplest way is first method.
我认为最简单的方法是第一种方法。