javascript 在 chrome 中工作时 string.contains() 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19196337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:43:56  来源:igfitidea点击:

string.contains() doesn't exist while working in chrome

javascriptgoogle-chrome

提问by mehrandvd

I have a code like:

我有一个类似的代码:

var valid = viewName.contains('/');

which works fine in firefox browser. But in chrome it is undefined. Why is that so? Is it true that chrome has not such a method for string?

在 Firefox 浏览器中运行良好。但在 chrome 中它是undefined. 为什么呢?chrome 真的没有这样的字符串方法吗?

Is it OK to use indexOfinstead of contains, is it supported in all browsers?

可以使用indexOf代替contains,所有浏览器都支持吗?

回答by Rajesh

Browser compatibility of String.contains()

String.contains() 的浏览器兼容性

String.indexOf()is what I use and it will work fine.

String.indexOf()是我使用的,它会正常工作。

  var strIndex = viewName.indexOf('/');
  if(strIndex == -1) {
     //string not found
  } else {
    //string found
  }

But, just in case you want to have a contains()function, you can add it to Stringas below:

但是,万一你想要一个contains()函数,你可以将它添加到String如下:

 if(!('contains' in String.prototype)) {
       String.prototype.contains = function(str, startIndex) {
                return -1 !== String.prototype.indexOf.call(this, str, startIndex);
       };
 }

var valid = viewName.contains('/');
if(valid) {
  //string found
} else {
  //string not found
}

回答by GrayedFox

Support for this, in Firefox and Chrome too, is now disabled by default. If you land here looking for an up to date answer, you can view the reason why, and the new method name (which is String.includes) here.

在 Firefox 和 Chrome 中对此的支持现在默认是禁用的。如果你的土地在这里寻找一个最新的回答,您可以查看的原因所在,而新方法的名称(这是String.includes在这里

Try:

尝试:

yourString.includes('searchString')

yourString.includes('searchString')

回答by Chris

.contains was entirely removed in FireFox 48 as the documentation, currently last updated on 22.07.2016, reveals. The function .includes does what .contains did before, though.

.contains 在 FireFox 48 中被完全删除,因为文档显示,目前最后更新时间为 2016 年 7 月 22 日。不过,函数 .includes 做了 .contains 之前做过的事情。

Benefits: .includes is also supported by Chrome.

优点:Chrome 也支持 .includes。

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includesfor reference.

请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes以供参考。

I was going to post this as comment but unregistered people can only write full answers. (facepalm)

我打算将此作为评论发布,但未注册的人只能写出完整的答案。(脸掌)

回答by Rob W

The containsmethod of strings was first added to V8, then included in Chromeafter version 30.0.1583.0, but it is disabled by default. This feature is only availableif you enable "experimental JavaScript features" at chrome://flags/#enable-javascript-harmony.

contains字符串的方法,第一次加入到V8,则Chrome的30.0.1583.0版本,但默认情况下禁用。此功能在您启用“实验性 JavaScript 功能”时可用chrome://flags/#enable-javascript-harmony