js index of()用法

Posted fanyee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js index of()用法相关的知识,希望对你有一定的参考价值。

含义:

indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。(工作中常用)

提示和注释:

注释:indexOf() 方法对大小写敏感

注释:如果要检索的字符串值没有出现,则该方法返回 -1。

实例

我们将在 "Hello world!" 字符串内进行不同的检索:

<script type="text/javascript">    
var str="Hello world!";  
document.write(str.indexOf("Hello") + "<br />");  
document.write(str.indexOf("World") + "<br />"); 
document.write(str.indexOf("world"));    
</script>

 

以上代码的输出:

0  -1  6   

 

多数用途

可以用于邮箱是否正确的判断,例如
<script type="text/javascript">    
if((yx.email.value.indexOf(@,0)==-1)||(yx.email.value.indexOf(.,0)==-1))
{
alter("邮箱地址错误");
yx.email.focus();
return false;
}
</script>

 

 另一个例子

在vue中的props

// 更好的做法!
props: {
  status: {
    type: String,
    required: true,
    validator: function (value) {
      return [
        ‘syncing‘,
        ‘synced‘,
        ‘version-conflict‘,
        ‘error‘
      ].indexOf(value) !== -1
    }
  }
}

 

以上是关于js index of()用法的主要内容,如果未能解决你的问题,请参考以下文章

Failed to convert property value of type ‘java.lang.String‘ to required type ‘int‘ for property(代码片段

报错:✘ http://eslint.org/docs/rules/indent Expected indentation of 0 s paces but found 2(代码片段

报错:✘ http://eslint.org/docs/rules/indent Expected indentation of 0 s paces but found 2(代码片段

forEach,for in,for of循环的用法

微信小程序第七天WXML语法之模板用法

微信小程序第七天WXML语法之模板用法