aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/lastindexof/index.html
blob: 3c3e51d6667d3335c6eea321fc5ad97db623240b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---
title: String.prototype.lastIndexOf()
slug: Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
translation_of: Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
---
<p><span class="hidden"> </span>{{JSRef}}</p>

<p> <strong><code>lastIndexOf()</code></strong> 方法返回调用{{jsxref("String")}} 对象的指定值最后一次出现的索引,在一个字符串中的指定位置 <code>fromIndex</code>处从后向前搜索。如果没找到这个特定值则返回-1 。</p>

<p>该方法将从尾到头地检索字符串 <em>str</em>,看它是否含有子串 <em>searchValue</em>。开始检索的位置在字符串的 <em>fromIndex</em> 处或字符串的结尾(没有指定 <em>fromIndex</em> 时)。如果找到一个 <em>searchValue</em>,则返回 <em>searchValue</em> 的第一个字符在 <em>str</em> 中的位置。<em>str</em>中的字符位置是从 0 开始的。</p>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="syntaxbox"><code><em>str</em>.lastIndexOf(<em>searchValue</em>[, fromIndex])</code></pre>

<h3 id="Parameters" name="Parameters">参数</h3>

<dl>
 <dt><code>searchValue</code></dt>
 <dd>一个字符串,表示被查找的值。如果<code>searchValue</code>是空字符串,则返回<code>fromIndex</code></dd>
 <dt><code>fromIndex</code>{{optional_inline}}</dt>
 <dd>待匹配字符串searchValue的开头一位字符从 str的第fromIndex位开始向左回向查找。<code>fromIndex</code>默认值是 <code>+Infinity</code>。如果 <code>fromIndex &gt;= str.length</code> ,则会搜索整个字符串。如果 <code>fromIndex &lt; 0</code> ,则等同于 <code>fromIndex == 0</code></dd>
 <dt>
 <h3 id="返回值">返回值</h3>
 </dt>
</dl>

<p>返回指定值最后一次出现的索引(该索引仍是以从左至右0开始记数的),如果没找到则返回-1。</p>

<h2 id="Description" name="Description">描述</h2>

<p>字符串中的字符被从左向右索引。首字符的索引(index)是 0,最后一个字符的索引是 <code>stringName.length - 1</code></p>

<pre><code>'canal'.lastIndexOf('a');     // returns 3 (没有指明fromIndex则从末尾l处开始反向检索到的第一个a出现在l的后面,即index为3的位置)
'canal'.lastIndexOf('a', 2);  // returns 1(指明fromIndex为2则从n处反向向回检索到其后面就是a,即index为1的位置)
'canal'.lastIndexOf('a', 0);  // returns -1(指明fromIndex为0则从c处向左回向检索a发现没有,故返回-1)
'canal'.lastIndexOf('x');     // returns -1
'canal'.lastIndexOf('c', -5); // returns 0(指明fromIndex为-5则视同0,从c处向左回向查找发现自己就是,故返回0)
'canal'.lastIndexOf('c', 0);  // returns 0(指明fromIndex为0则从c处向左回向查找c发现自己就是,故返回自己的索引0)
'canal'.lastIndexOf('');      // returns 5
'canal'.lastIndexOf('', 2);   // returns 2</code>
</pre>

<div class="blockIndicator note">
<p>Note: <code>'abab'.lastIndexOf('ab', 2)</code> 将返回 2 而不是 0, 因为fromIndex只限制待匹配字符串的开头。</p>
</div>

<p>(例如'abadefgabm'.lastIndexOf('ab', 7) 返回7,虽然查找的'ab'中的b已经在 index=8的位置了从index=7的a处向左查找仍是能找到自身a加上其后连成ab,因为fromIndex指的是待匹配字符串的开头那一个)</p>

<h3 id="Example_indexOf_and_case-sensitivity" name="Example:_indexOf_and_case-sensitivity">区分大小写</h3>

<p><code>lastIndexOf</code> 方法区分大小写。例如,下面的表达式返回 -1:</p>

<pre class="brush:js">"Blue Whale, Killer Whale".lastIndexOf("blue"); // returns -1</pre>

<h2 id="Examples" name="Examples">示例</h2>

<h3 id="Example_Using_indexOf_and_lastIndexOf" name="Example:_Using_indexOf_and_lastIndexOf">例子:使用 <code>indexOf</code><code>lastIndexOf</code></h3>

<p>下例使用 <code>indexOf</code> 和 <code>lastIndexOf</code> 方法来定位字符串 "<code>Brave new world</code>" 中的值。</p>

<pre class="brush:js">var anyString = "Brave new world";

console.log("The index of the first w from the beginning is " + anyString.indexOf("w"));
// Displays 8
console.log("The index of the first w from the end is " + anyString.lastIndexOf("w"));
// Displays 10

console.log("The index of 'new' from the beginning is " + anyString.indexOf("new"));
// Displays 6
console.log("The index of 'new' from the end is " + anyString.lastIndexOf("new"));
// Displays 6
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.5.4.8', 'String.prototype.lastIndexOf')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-string.prototype.lastindexof', 'String.prototype.lastIndexOf')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>{{jsxref("String.prototype.charAt()")}}</li>
 <li>{{jsxref("String.prototype.indexOf()")}}</li>
 <li>{{jsxref("String.prototype.split()")}}</li>
 <li>{{jsxref("Array.prototype.indexOf()")}}</li>
 <li>{{jsxref("Array.prototype.lastIndexOf()")}}</li>
</ul>