JavaScript 字符串搜索
字符串搜索方法
JavaScript String indexOf()
The indexOf()
方法返回字符串中第一个出现的字符串的索引(位置),或者如果字符串未找到,则返回 -1
注意
JavaScript 从零开始计数位置。
0 是字符串中的第一个位置,1 是第二个,2 是第三个,...
JavaScript String lastIndexOf()
The lastIndexOf()
方法返回字符串中最后出现的指定文本的索引
Both indexOf()
和 lastIndexOf()
如果文本未找到,则返回 -1
两种方法都接受第二个参数作为搜索的起始位置
The lastIndexOf()
方法从后向前搜索(从结尾到开头),这意味着:如果第二个参数是 15
,搜索将从位置 15 开始,并搜索到字符串的开头。
JavaScript String search()
The search()
method searches a string for a string (or a regular expression) and returns the position of the match
Examples
let text = "Please locate where 'locate' occurs!";
text.search("locate");
自己试试 »
let text = "Please locate where 'locate' occurs!";
text.search(/locate/);
自己试试 »
Did You Notice?
The two methods, indexOf()
and search()
, are equal?
They accept the same arguments (parameters), and return the same value?
The two methods are NOT equal. These are the differences
- The
search()
method cannot take a second start position argument. - The
indexOf()
method cannot take powerful search values (regular expressions).
You will learn more about regular expressions in a later chapter.
JavaScript String match()
The match()
method returns an array containing the results of matching a string against a string (or a regular expression).
Examples
Perform a search for "ain"
let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain");
自己试试 »
Perform a search for "ain"
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/);
自己试试 »
Perform a global search for "ain"
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
自己试试 »
Perform a global, case-insensitive search for "ain"
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
自己试试 »
注意
If a regular expression does not include the g modifier (global search), match()
will return only the first match in the string.
Read more about regular expressions in the chapter JS RegExp.
JavaScript String matchAll()
The matchAll()
method returns an iterator containing the results of matching a string against a string (or a regular expression).
If the parameter is a regular expression, the global flag (g) must be set, otherwise a TypeError is thrown.
If you want to search case insensitive, the insensitive flag (i) must be set
JavaScript String includes()
The includes()
method returns true if a string contains a specified value.
Otherwise it returns false
.
Examples
Check if a string includes "world"
let text = "Hello world, welcome to the universe.";
text.includes("world");
自己试试 »
Check if a string includes "world". Start at position 12
let text = "Hello world, welcome to the universe.";
text.includes("world", 12);
自己试试 »
Notes
includes()
is case sensitive.
includes()
is an ES6 feature.
includes()
is not supported in Internet Explorer.
JavaScript String startsWith()
The startsWith()
method returns true
if a string begins with a specified value.
Otherwise it returns false
Examples
Returns true
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello");
自己试试 »
Returns false
let text = "Hello world, welcome to the universe.";
text.startsWith("world")
自己试试 »
A start position for the search can be specified
Returns false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5)
自己试试 »
Returns true
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 6)
自己试试 »
Notes
startsWith()
is case sensitive.
startsWith()
is an ES6 feature.
startsWith()
is not supported in Internet Explorer.
JavaScript String endsWith()
The endsWith()
method returns true
if a string ends with a specified value.
Otherwise it returns false
Examples
Check if a string ends with "Doe"
let text = "John Doe";
text.endsWith("Doe");
自己试试 »
Check if the 11 first characters of a string ends with "world"
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);
Notes
endsWith()
is case sensitive.
endsWith()
is an ES6 feature.
endsWith()
is not supported in Internet Explorer.
Complete String Reference
For a complete String reference, go to our
Complete JavaScript String Reference.
The reference contains descriptions and examples of all string properties and methods.