Get to know MDN better
このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
slice() は String 値のメソッドで、元の文字列を変更せず、文字列の一部分を取り出し、それを新しい文字列として返します。
返却する部分文字列に含める最初の文字の位置です。
indexEnd 省略可返却する部分文字列から除外する最初の文字の位置です。
文字列の取り出された部分を含んだ新しい文字列です。
slice() は 1 つの文字列からテキストを取り出し、新しい文字列を返します。
slice() は、indexEnd まで(ただし indexEnd 自体は含まない)を抽出します。例えば、str.slice(4, 8) は、5 文字目から 8 文字目(インデックス 4、5、6、7 の文字)を抽出します。
indexStart indexEnd ↓ ↓ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | | T | h | e | | m | i | r | r | o | r | m i r r _______________ ↑ Result以下の例では、新しい文字列を生成するために slice() を使っています。
下記の例では負の位置で slice() を使っています。
この例では、文字列の末尾から前方に 11 番目を開始位置とし、先頭から後方に 16 番目を終了位置とします。
こちらは先頭から後方に 11 番目を開始位置とし、末尾から前方に 7 番目を終了位置とします。
これらの引数は、末尾から前方に 5 番目を開始位置とし、末尾から前方に 1 番目を終了位置とします。
| ECMAScript® 2027 Language Specification # sec-string.prototype.slice |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年8月22日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.