如何定位字符串的特定部分

How to target a specific part of string?

本文关键字:定部 字符串 何定位 定位      更新时间:2023-09-26

返回模式以字符串开头的索引(如果未找到,则返回 -1)。如果第三个参数为 true,则搜索区分大小写,否则不区分大小写。

Examples
index("abAB12","AB",true) returns 2 but index("abAB12","AB",false) returns 0
index("abAB12","BA",true) returns -1 and index("abAB12","BA",false) returns 1

也许是这样的:

var str = "abAB12";
var i = str.indexOf("AB");
if (i < str.length / 2) {
  //stuff
} else {
  //other stuff
}