aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/string/lastindexof/index.html
blob: d9ae2f826073fcf5e10b6aa632d641c9d9c0dc01 (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
---
title: String.prototype.lastIndexOf()
slug: Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
tags:
  - JavaScript
  - Method
  - Prototype
  - Reference
  - String
  - lastIndexOf
translation_of: Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
---
<div>{{JSRef}}</div>

<p><span class="seoSummary"><strong><code>lastIndexOf()</code></strong> メソッドは、呼び出した {{jsxref("String")}} オブジェクトの中で、 <code>fromIndex</code> から前方向に検索を始め、指定された値が最後に現れたインデックスを返します。値が見つからない場合は <code>-1</code> を返します。</span></p>

<div>{{EmbedInteractiveExample("pages/js/string-lastindexof.html", "shorter")}}</div>

<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div>

<h2 id="Syntax" name="Syntax">構文</h2>

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

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

<dl>
 <dt><code><var>searchValue</var></code></dt>
 <dd>検索する値を表す文字列です。 <code><var>searchValue</var></code> が空文字列であった場合は、 <code><var>fromIndex</var></code> を返します。
 </dd><dt><code><var>fromIndex</var></code> {{optional_inline}}</dt>
 <dd>比較の先頭とみなされる文字列の最後の文字の位置です。既定値は <code>+Infinity</code> です。 <code><var>fromIndex</var> &gt;= <var>str</var>.length</code> の場合、文字列全体が検索されます。 <code><var>fromIndex</var> &lt; 0</code> の場合は、 <code>0</code> の場合と同じ動作になります。
</dd></dl>

<h3 id="Return_value" name="Return_value">返値</h3>

<p><code><var>searchValue</var></code> が最後に出現した位置です。見つからなかった場合は、 <code>-1</code> になります。</p>

<h2 id="Description" name="Description">解説</h2>

<p>文字列における文字は左から右にインデックス化されます。一番最初の文字の位置は <code>0</code> で、一番最後の文字は <code><var>str</var>.length - 1</code> です。</p>

<pre class="brush: js notranslate">'canal'.lastIndexOf('a');     // returns 3
'canal'.lastIndexOf('a', 2);  // 1 を返す
'canal'.lastIndexOf('a', 0);  // -1 を返す
'canal'.lastIndexOf('x');     // -1 を返す
'canal'.lastIndexOf('c', -5); // 0 を返す
'canal'.lastIndexOf('c', 0);  // 0 を返す
'canal'.lastIndexOf('');      // 5 を返す
'canal'.lastIndexOf('', 2);   // 2 を返す
</pre>

<div class="note">
<p><strong>注:</strong> <code>'abab'.lastIndexOf('ab', 2)</code><code>2</code> を返し、 <code>0</code> にはなりません。 <code><var>fromIndex</var></code> は検索の開始位置を制約するものだからです。</p>
</div>

<h3 id="Case-sensitivity" name="Case-sensitivity">大文字と小文字の区別</h3>

<p><code>lastIndexOf()</code> メソッドは大文字と小文字を区別します。例えば、以下の式は <code>-1</code> を返します。</p>

<pre class="brush: js notranslate">'Blue Whale, Killer Whale'.lastIndexOf('blue'); // -1 を返す
</pre>

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

<h3 id="Using_indexOf_and_lastIndexOf" name="Using_indexOf_and_lastIndexOf">indexOf() と lastIndexOf() の使用</h3>

<p>以下の例は、 {{jsxref("String.prototype.indexOf()", "indexOf()")}}<code>lastIndexOf()</code> を使用して文字列 "<code>Brave new world</code>" の中の値の位置を示します。</p>

<pre class="brush: js notranslate">let anyString = 'Brave new world';

console.log('先頭から見て最初に w が出現する位置: ' + anyString.indexOf('w'));
// 8 と出力
console.log('末尾から見て最初に w が出現する位置: ' + anyString.lastIndexOf('w'));
// 10 と出力
console.log('先頭から見た "new" の位置: ' + anyString.indexOf('new'));
// 6 と出力
console.log('末尾から見た "new" の位置: ' + anyString.lastIndexOf('new'));
// 6 と出力
</pre>

<h2 id="Specifications" name="Specifications">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-string.prototype.lastindexof', 'String.prototype.lastIndexOf')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>

<p>{{Compat("javascript.builtins.String.lastIndexOf")}}</p>

<h2 id="See_also" name="See_also">関連情報</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>