js判断字符串是否包含指定的字符

 

判断字符串是否包含指定字符是很经常使用的功能,好比说,注册时用户名限制不能输入“管理员”,或者须要js判断url跳转连接是否包含某个关键词等……javascript

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script type="text/javascript">
    /*输出的数字表明位置,若是值为-1则表示没有这个字符
     *注意:字母的话区别大小写
     */
    a ="大鱼海棠的博客:http://www.cnblogs.com/leeLxj/"
    b = "鱼"
    document.write(a.indexOf(b)+"<br/>");
    b= "海棠"
    document.write(a.indexOf(b)+"<br/>");
    b= "yu"
    document.write(a.indexOf(b)+"<br/>");
    b= "tang"
    document.write(a.indexOf(b)+"<br/>");
</script>

</body>
</html>
相关文章
相关标签/搜索