このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
js:build-in:string [2021/09/06 11:19] y2sunlight |
js:build-in:string [2021/09/13 11:02] (現在) y2sunlight |
||
---|---|---|---|
行 8: | 行 8: | ||
* [[# | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
* [[# | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
* [[# | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
* [[# | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
* [[# | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
+ | * [[# | ||
* [[# | * [[# | ||
- | * [[# | ||
* [[# | * [[# | ||
* [[# | * [[# | ||
行 176: | 行 202: | ||
<sxh javascript; | <sxh javascript; | ||
let str = " | let str = " | ||
- | console.log(str.toLowerCase()); // HELLO, SATO. | + | console.log(str.toUpperCase()); // HELLO, SATO. |
</ | </ | ||
行 237: | 行 263: | ||
let str = " こんにちは "; | let str = " こんにちは "; | ||
console.log(str.trimEnd()); | console.log(str.trimEnd()); | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | === charAt() === | ||
+ | |||
+ | <code javascript> | ||
+ | let character = str.charAt(index) | ||
+ | </ | ||
+ | |||
+ | 指定された位置の文字 (UTF-16 コード 1 つから成ります) を返します。詳しくは[[https:// | ||
+ | |||
+ | <sxh javascript; | ||
+ | let str = " | ||
+ | console.log(str.charAt(6)); | ||
</ | </ | ||
行 276: | 行 316: | ||
<code javascript> | <code javascript> | ||
- | lastIndexOf(searchValue) | + | lastIndexOf(searchValue[, fromIndex]) |
- | lastIndexOf(searchValue, fromIndex) | + | |
</ | </ | ||
行 331: | 行 370: | ||
<sxh javascript; | <sxh javascript; | ||
let csv = " | let csv = " | ||
- | console.log(csv.split(',' | + | console.log(csv.split(',' |
</ | </ | ||
行 338: | 行 377: | ||
<sxh javascript; | <sxh javascript; | ||
let csv = " | let csv = " | ||
- | console.log(csv.split(/ | + | console.log(csv.split(/ |
</ | </ | ||
行 353: | 行 392: | ||
<sxh javascript; | <sxh javascript; | ||
let str = " | let str = " | ||
- | console.log(str.substr(6, | + | console.log(str.substr(6, |
</ | </ | ||
行 366: | 行 405: | ||
(1)の場合( length < 0 )、substr は、空文字を返します。 | (1)の場合( length < 0 )、substr は、空文字を返します。 | ||
- | (2)の場合( start < 0 )、substr は、負数( -5 )を文字列末尾からのインデックスと見なします。従って '' | + | (2)の場合( start < 0 )、substr は、負数( -5 )を文字列末尾からのインデックスと見なします。従って '' |
\\ | \\ | ||
行 392: | 行 431: | ||
</ | </ | ||
- | (1)の場合( start < end )、substring は、start と end を入れ替えます。従って '' | + | (1)の場合( start < end )、substring は、start と end を入れ替えます。従って '' |
- | (2)の場合( end < 0 )、substring は、まず負数( -1 )を 0 に置き換えた後、( start < end )なので、start と end を入れ替えます。従って '' | + | (2)の場合( end < 0 )、substring は、まず負数( -1 )を 0 に置き換えた後、( start < end )なので、start と end を入れ替えます。従って '' |
\\ | \\ | ||
行 422: | 行 461: | ||
(1)の場合( start < end )、slice は、空文字を返します。 | (1)の場合( start < end )、slice は、空文字を返します。 | ||
- | (2)の場合( end < 0 )、slice は、負数( -1 )を文字列末尾からのインデックスと見なします。従って '' | + | (2)の場合( end < 0 )、slice は、負数( -1 )を文字列末尾からのインデックスと見なします。従って '' |
\\ | \\ | ||
行 449: | 行 488: | ||
</ | </ | ||
- | 現在の文字列の先頭から指定した文字列で埋めた、長さ targetLength | + | 先頭を padString |
<sxh javascript; | <sxh javascript; | ||
+ | let str = " | ||
+ | console.log(str.padStart(5)); | ||
</ | </ | ||
行 462: | 行 503: | ||
</ | </ | ||
- | 現在の文字列の末尾から指定された文字列で埋めた、長さ targetLength | + | 末尾を padString |
<sxh javascript; | <sxh javascript; | ||
+ | let str = " | ||
+ | console.log(str.padEnd(5)); | ||
</ | </ | ||
行 475: | 行 518: | ||
</ | </ | ||
- | オブジェクトの要素を | + | count 回繰り返した文字列を返します。詳しくは[[https:// |
<sxh javascript; | <sxh javascript; | ||
+ | let str = " | ||
+ | console.log(str.repeat(3)); | ||
</ | </ | ||
行 490: | 行 535: | ||
</ | </ | ||
- | 文字列に対して正規表現 regexp を指定し、合致した部分文字列を取得します。詳しくは[[https:// | + | 文字列に対して正規表現 regexp を指定し、合致した部分文字列を取得します。 |
<sxh javascript; | <sxh javascript; | ||
- | let str = " | + | let str = " |
- | console.log(str.match(/ | + | console.log(str.match(/ |
</ | </ | ||
+ | |||
+ | /g フラグの有無によって返される値が異なります。上例では合致した部分文字列の配列が返されます。詳しくは[[https:// | ||
\\ | \\ | ||
- | === matchAll() === | + | === search() === |
<code javascript> | <code javascript> | ||
- | str.matchAll(regexp) | + | str.search(regexp) |
</ | </ | ||
- | regexp | + | 文字列に対してい正規表現 |
<sxh javascript; | <sxh javascript; | ||
+ | let str = " | ||
+ | console.log(str.search(/ | ||
</ | </ | ||
行 527: | 行 576: | ||
\\ | \\ | ||
- | === replaceAll() === | + | === matchAll() === |
<code javascript> | <code javascript> | ||
- | const newStr = str.replaceAll(regexp|substr, newSubstr|function) | + | str.matchAll(regexp) |
</ | </ | ||
- | searchFor | + | キャプチャグループを含む regexp |
<sxh javascript; | <sxh javascript; | ||
+ | let str = " | ||
+ | let matches = str.matchAll(/ | ||
+ | for(match of matches) { | ||
+ | console.log(match); | ||
+ | } | ||
+ | // 出力結果 | ||
+ | // [" | ||
+ | // [" | ||
+ | // [" | ||
</ | </ | ||
\\ | \\ | ||
- | === search() === | + | === replaceAll() === |
<code javascript> | <code javascript> | ||
- | str.search(regexp) | + | const newStr = str.replaceAll(searchFor, replaceWith) |
</ | </ | ||
- | 文字列に対してい正規表現 | + | searchFor が現れたところすべてを replaceWith で置換するために使用します。 searchFor は文字列または正規表現であり、 replaceWith は文字列または関数です。詳しくは[[https:// |
<sxh javascript; | <sxh javascript; | ||
- | let str = " | + | let str = " |
- | console.log(str.search(/ | + | console.log(str.replaceAll(/(佐藤|鈴木|高橋)/g, ' |
</ | </ | ||
行 556: | 行 614: | ||
==== コード変換 ==== | ==== コード変換 ==== | ||
- | |||
- | === charAt() === | ||
- | |||
- | <code javascript> | ||
- | let character = str.charAt(index) | ||
- | </ | ||
- | |||
- | 指定された位置の文字 (UTF-16 コード 1 つから成ります) を返します。詳しくは[[https:// | ||
- | |||
- | <sxh javascript; | ||
- | let str = " | ||
- | console.log(str.charAt(6)); | ||
- | </ | ||
- | |||
- | \\ | ||
=== charCodeAt() === | === charCodeAt() === |