我要搜索的JavaScript字符串,看它是否含有某些字符
<script type="text/javascript">
var s="hello world";
if(s.contains("hello")) alert("contains");
</script>
我可能期望像函数string.contains()。 是他们的一个string.contains在JavaScript或我应该使用indexOf而不是
我要搜索的JavaScript字符串,看它是否含有某些字符
<script type="text/javascript">
var s="hello world";
if(s.contains("hello")) alert("contains");
</script>
我可能期望像函数string.contains()。 是他们的一个string.contains在JavaScript或我应该使用indexOf而不是
indexOf是要走的路 - 只是检查返回值。
尝试正则表达式:
if(/hello/.test(s))
alert("contains");
如果你真的想要一个包含:
String.prototype.contains = function (searchTerm) {
return this.toString().indexOf(searchTerm) !== -1;
};
如果你感觉花哨:
String.prototype.contains = function (searchTerm) {
return ~this.toString().indexOf(searchTerm);
};
http://stackoverflow.com/questions/1789945/javascript-string-contains复制 – VirtualTroll
这一直以前问过很多次,你应该在提问之前先搜索一下 – jbabey