如何在JavaScript中检查字符串是否包含子字符串?

栏目: JavaScript · 发布时间: 7年前

内容简介:这里有一个可用方法列表:性能测试表明,如果速度很重要,indexOf可能是最好的选择。:

这里有一个可用方法列表:

  1. (ES6) includes
var string = "foo",  
    substring = "oo";
string.includes(substring);
  1. ES5 and older indexOf
var string = "foo",  
    substring = "oo";
string.indexOf(substring) !== -1;  
String.prototype.indexOf returns the position of the string in the other string. If not found, it will return -1.
  1. search
var string = "foo",  
    expr = /oo/;
string.search(expr);
  1. lodash includes
var string = "foo",  
    substring = "oo";
_.includes(string, substring);
  1. RegExp
var string = "foo",  
    expr = /oo/;  // no quotes here
expr.test(string);
  1. Match
var string = "foo",  
    expr = /oo/;
string.match(expr);

性能测试表明,如果速度很重要,indexOf可能是最好的选择。

转载请注明出处

http://zgljl2012.com/ru-he-zai-javascriptzhong-jian-cha-zi-fu-chuan-shi-fou-bao-han-zi-zi-fu-chuan/


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Python自然语言处理

Python自然语言处理

(英)伯德、(英)克莱因、(美)洛普 / 东南大学出版社 / 2010-6 / 64.00元

《Python自然语言处理(影印版)》提供了非常易学的自然语言处理入门介绍,该领域涵盖从文本和电子邮件预测过滤,到自动总结和翻译等多种语言处理技术。在《Python自然语言处理(影印版)》中,你将学会编写Python程序处理大量非结构化文本。你还将通过使用综合语言数据结构访问含有丰富注释的数据集,理解用于分析书面通信内容和结构的主要算法。 《Python自然语言处理》准备了充足的示例和练习,......一起来看看 《Python自然语言处理》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具