欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js 判斷字符串中是否包含某個字符串的實現(xiàn)代碼

 更新時間:2023年03月27日 00:11:02   投稿:mdxy-dxy  
工作中經(jīng)常會使用到判斷一個字符串是否包含某一個字符串,因此總結(jié)一下幾個方法,需要的朋友可以參考下

indexOf方法

根據(jù)國外大神的測試結(jié)果顯示,indexOf在速度上可能是最快的,因此推薦大家在日常中還是經(jīng)常使用indexOf這個方法。

ES5或者更老版本

String.prototype.indexOf方法用來返回一個字符串在另一個字符串中的位置,如果沒找到那就返回 -1 。

代碼如下:

var string = "foo",
    substring = "oo";
string.indexOf(substring) !== -1;//true

如果大量使用可以定義一個函數(shù),方便調(diào)用

function instr(str, find) {
    if (str.indexOf(find) != -1) {
        return 1;
    } else {
        return 0;
    }
}

string.lastIndexOf()

和indexOf()的區(qū)別,從字符串的尾部開始查找

返回值:匹配成功的第一個字符的下標,未匹配則返回-1

用法:string.indexOf(searchValue, start)

let searchVal = 'yyds'
let searchResult = searchVal.lastIndexOf('y')
console.log(searchResult) // 1

ES6 includes 方法

返回值:Boolean

用法: string.includes(searchValue, start) 第二個參數(shù)從是指定下標開始查找

兼容補丁(polyfill)

if (!String.prototype.includes) {
  String.prototype.includes = function(search, start) {
    'use strict';
    if (typeof start !== 'number') {
      start = 0;
    }

    if (start + search.length > this.length) {
      return false;
    } else {
      return this.indexOf(search, start) !== -1;
    }
  };
}

示例

var str = 'To be, or not to be, that is the question.';

console.log(str.includes('To be'));       // true
console.log(str.includes('question'));    // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1));    // false
console.log(str.includes('TO BE'));       // false

當包含時返回true,不包含時返回false,代碼實例如下:

var string = "foo";
        var substring1 = "oo";
        var substring2 = "oq";
        string.includes(substring1); //true
        string.includes(substring2); //false
 let searchVal = 'yyds'
 let searchResult = searchVal.includes('y', 1)
 console.log(searchResult) // true

string.search()

定義和用法

search() 方法將字符串與正則表達式匹配。

注釋:如果搜索值為字符串,則轉(zhuǎn)換為正則表達式。

search() 方法返回第一個匹配項的索引(位置)。

如果未找到匹配項,則 search() 方法返回 -1。

提示:search() 方法區(qū)分大小寫。

返回值:匹配成功的第一個字符的下標,未匹配則返回-1

用法:string.search(searchValue)

search方法與indexOf類似,用來返回一個字符串在另一個字符串中的位置,如果沒找到那就返回-1,。

唯一需要注意的是,search方法的參數(shù)是一個正則表達式。

代碼如下:

var string = "foo",
    expr = /oo/;
string.search(expr);// 返回1
 let searchVal = 'yyds'
 let searchResult = searchVal.search('y')
 console.log(searchResult) // 0  

不區(qū)分大小寫的搜索

let text = "Mr. Blue has a blue house";
let position = text.search(/blue/i);

lodash includes方法

使用Javascript工具庫lodash的includes方法,該方法返回一個布爾值,代碼如下:

_.includes('foobar', 'ob'); 

// → true

RegExp正則表達式

使用正則表達式的match來判斷是否包含,代碼如下:

var string = "foo",
    expr = /oo/;  // no quotes here
expr.test(string);//true

Match方法

使用match的方法,該方法字符串不匹配的話就返回null,代碼如下:

var string = "foo",
    expr = /oo/,
    expr2 = /oa/;
string.match(expr);//["oo", index: 1, input: "foo", groups: undefined]
string.match(expr2);//null

到此這篇關(guān)于js 判斷字符串中是否包含某個字符串的實現(xiàn)代碼的文章就介紹到這了,更多相關(guān)字符串中是否包含某個字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論