微信小程序

JS判断一个字符串是否包含一个子串函数.

//str 字符串,name子串

    contains:function(str,name){

        if(str.indexOf(name) > -1){

         return 1;

        }else{

            return 0;

        }

    }

 

 

一、String.indexOf()

var str = "access,substance,account,competition,obvious";

var name = "access";

if(str.indexOf(name) > -1){

    alert("contains...");

}
					

二、正则

var str = " access,substance,account,competition,obvious
							

var b = new
								RegExp("access").test(str);

if(b){

    alert("contains...");

}
			

 

文章来源:刘俊涛的博客

地址:http://www.cnblogs.com/lovebing

欢迎关注,有问题一起学习欢迎留言、评论。

相关文章:

  • 2022-01-08
  • 2021-11-17
  • 2021-06-20
  • 2022-12-23
  • 2021-12-28
  • 2022-01-07
猜你喜欢
  • 2021-07-31
  • 2022-12-23
  • 2021-09-01
  • 2021-07-25
  • 2021-09-19
  • 2021-12-12
  • 2022-12-23
相关资源
相似解决方案