js 判斷字符串中是否包含某個字符串的實現(xiàn)代碼
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');
// → trueRegExp正則表達式
使用正則表達式的match來判斷是否包含,代碼如下:
var string = "foo",
expr = /oo/; // no quotes here
expr.test(string);//trueMatch方法
使用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)文章希望大家以后多多支持腳本之家!
- JS中判斷某個字符串是否包含另一個字符串的五種方法
- js判斷一個字符串是否包含一個子串的方法
- JS獲取字符串實際長度(包含漢字)的簡單方法
- javascript 判斷字符串是否包含某字符串及indexOf使用示例
- js判斷數(shù)組是否包含某個字符串變量的實例
- 用javascript實現(xiàn)截取字符串包含中文處理的函數(shù)
- JS判斷字符串包含的方法
- js計算字符串長度包含的中文是utf8格式
- JS實現(xiàn)快速比較兩個字符串中包含有相同數(shù)字的方法
- JS不用正則驗證輸入的字符串是否為空(包含空格)的實現(xiàn)代碼
- JavaScript判斷一個字符串是否包含指定子字符串的方法
- JS中append字符串包含onclick無效傳遞參數(shù)失敗的解決方案
- JS給Array添加是否包含字符串的簡單方法
- js處理包含中文的字符串實例
- javascript自動生成包含數(shù)字與字符的隨機字符串
- JavaScript獲取字符串實際長度(包含中英文)
- JavaScript面試中常考的字符串操作方法大全(包含ES6)
- JavaScript字符串包含問題
相關(guān)文章
在ASP.NET MVC項目中使用RequireJS庫的用法示例
這篇文章主要介紹了在ASP.NET MVC項目中使用RequireJS的用法示例,文中主要講解了網(wǎng)站項目的一些基本目錄結(jié)構(gòu)思想,并給出了一個半自動壓縮的例子,的朋友可以參考下2016-02-02
easyUI實現(xiàn)(alert)提示框自動關(guān)閉的實例代碼
下面小編就為大家?guī)硪黄猠asyUI實現(xiàn)(alert)提示框自動關(guān)閉的實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
bootstrap table插件的分頁與checkbox使用詳解
這篇文章主要為大家詳細介紹了bootstrap table插件的分頁與checkbox使用詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07

