方法一:var str = "hello world"; if(str.indexOf("world") > -1 ) { console.log('str 中包含world字符串'); }
lastIndexOf() 的用法与indexOf()相同,只是从右向左查找,查找的起始方向不同。
方法二:var str = "hello world"; var patt = new RegExp("world"); var result = patt.test(str); console.log("Result: " + result);
result的值为true