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
tags:
- JavaScript
- Prototipo
- Referencia
- String
- lastIndexOf
- metodo
translation_of: Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
---
<div>{{JSRef}}</div>
<p>O método <code>lastIndexOf()</code> retorna o índice da última ocorrência do valor especificado encontrado na {{jsxref("String")}}. Quando <code>fromIndex</code> é especificado, a pesquisa é realizada de trás para frente. Retorna <code>-1</code> se o valor não for encontrado.</p>
<h2 id="Sintaxe">Sintaxe</h2>
<pre class="syntaxbox notranslate"><code><var>str</var>.lastIndexOf(<var>searchValue</var>[, <var>fromIndex</var>])</code></pre>
<h3 id="Parâmetros">Parâmetros</h3>
<dl>
<dt><code>searchValue</code></dt>
<dd>Uma string representando o valor a ser procurado. Se <code>searchValue</code> for uma string vazia, <code>str.length</code> é retornado.</dd>
<dt><code>fromIndex</code></dt>
<dd>Opcional. O índice no qual a pesquisa será iniciada de trás para frente. O valor padrão é <code>+Infinity</code>. Se <code>fromIndex >= str.length</code>, toda string é pesquisada. Se <code>fromIndex < 0</code>, o comportamento será o mesmo que seria com o índice <code>0</code>.</dd>
</dl>
<h3 id="Valor_retornado">Valor retornado</h3>
<p>O índice da última ocorrência referente ao valor especificado em <code>searchValue</code>. É retornado <code>-1</code> se nada for encontrado.</p>
<h2 id="Descrição">Descrição</h2>
<p>Os caracteres em uma string são indexados da esquerda para a direita. O índice do primeiro caractere é <code>0</code>, e o índice do último caractere é <code>str.length - 1</code>.</p>
<pre class="brush: js notranslate">'ricardo'.lastIndexOf('r'); // retorna 4
'ricardo'.lastIndexOf('a', 3); // retorna 3
'ricardo'.lastIndexOf('a', 0); // retorna -1
'ricardo'.lastIndexOf('x'); // retorna -1
'ricardo'.lastIndexOf('r', -5); // retorna 0
'ricardo'.lastIndexOf('r', 0); // retorna 0
'ricardo'.lastIndexOf(''); // retorna 7
'ricardo'.lastIndexOf('', 2); // retorna 2
</pre>
<h3 id="Sensível_a_maiúsculas_e_minúsculas">Sensível a maiúsculas e minúsculas</h3>
<p>O método <code>lastIndexOf()</code> é sensível a letras maiúsculas e minúsculas. Por exemplo, a seguinte expressão retorna <code>-1</code>:</p>
<pre class="brush: js notranslate">'Blue Whale, Killer Whale'.lastIndexOf('blue'); // retorna -1
</pre>
<h2 id="Exemplos">Exemplos</h2>
<h3 id="Usando_lastIndexOf">Usando <code>lastIndexOf()</code></h3>
<p>O seguinte exemplo usa <code>lastIndexOf()</code> para localizar valores nas string <code>"Brave new world"</code>.</p>
<pre class="brush: js notranslate">var anyString = 'Brave new world';
console.log('O índice do primeiro w a partir do final é ' + anyString.lastIndexOf('w'));
// retorna 10
console.log('O índice de "new" a partir do final é ' + anyString.lastIndexOf('new'));
// retorna 6
</pre>
<h2 id="Especificações">Especificações</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Especificação</th>
<th scope="col">Estado</th>
<th scope="col">Comentários</th>
</tr>
<tr>
<td>{{SpecName('ES1')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Definição inicial.</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>
<tr>
<td>{{SpecName('ESDraft', '#sec-string.prototype.lastindexof', 'String.prototype.lastIndexOf')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="Navegadores_compatíveis">Navegadores compatíveis</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>Suporte básico</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>Suporte básico</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="Veja_também">Veja também</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>
|