ES6 - RegExp search()



此方法返回在字串中找到匹配項的索引。如果未找到匹配項,則返回 -1。

語法

str.replace(regexp|substr, newSubStr|function)           

引數詳情

  • 正則表示式 − 正則表示式物件。

  • 子字串 − 要替換的字串。

  • 新子字串 − 替換字串。

  • 函式 − 用於建立新字串的函式。

返回值

返回在字串中找到匹配項的索引。

示例

var str = 'Welcome to ES6.We are learning ES6'; 
var re = new RegExp(/We/); 
var found = str.search(re); 
console.log(found); 

輸出

0     
廣告