X hashtag: #esspec
便利ツール
時事ネタ
フロカン東京
フロカン福岡
自己紹介 (近況報告)
syumai syumai.icon
TSを書いて暮らしてます
TSKaigiで話してきました
iwatsurut
とくに、イベントもなく過ごしています。
igrep(山本悠滋)
関数型まつりの発表の日程が決まった。2026/07/12 15:30〜
maru。(まる)
主にTSで仕事してます。React, Node。
年内で家がなくなることになったのでどうしようか考え中
気になっているやつ
同時編集JavaScript Playground
前半
後半
前回のあらすじ
今回のメモ
matchAll
英語
5. Let truncatedStringFiller be the String value consisting of repeated concatenations of fillString truncated to length fillLen.
5. truncatedStringFiller を、fillString を繰り返して連結し、その長さを fillLen に切り詰めた文字列の値とする。
自然だ、、
code:js
// ===== Code =====
{
console.log(..."abcdeabcde".matchAll(/.c./g));
// g flagがついていないのでエラー
try {
console.log(..."abcdeabcde".matchAll(/.c./));
} catch(err) {
console.error(err);
}
}
// %Symbol.match% プロパティがあるパターン
{
const myRegExp = {
return 1;
},
return 2;
},
flags: "g" // これが必要
}
console.log("abcdeabcde".matchAll(myRegExp));
}
// %Symbol.match% プロパティがないパターン
{
const myRegExp = {
return 2;
},
}
console.log("abcdeabcde".matchAll(myRegExp));
}
// 引数が文字列のパターン
{
}
{
}
// ===== Output =====
bcd bcd
TypeError: String.prototype.matchAll called with a non-global RegExp argument
2
2
aa,aa,aa
null
code:js
// ===== Code =====
// padEnd
{
console.log("abc".padEnd(8));
console.log("abc".padEnd(8, "xy"));
}
// padStart
{
console.log("abc".padStart(8));
console.log("abc".padStart(8, "xy"));
}
// padEndの例外パターン
{
console.log(String.prototype.padStart.call(null, 5));
}
// ===== Output =====
abc
abcxyxyx
abc
xyxyxabc
TypeError: String.prototype.padStart called on null or undefined
懐かしのleft-pad事件
code:js
// ===== Code =====
{
console.log("abc".repeat(3));
console.log("0".repeat(2.5));
console.log("0".repeat(3.5));
console.log("0".repeat(-.5));
}
// intentionally generic
{
console.log(String.prototype.repeat.call({}, 3));
}
// 例外パターン
{
try {
console.log(String.prototype.repeat.call(null, 3));
} catch (err) {
console.error(err)
}
try {
console.log("0".repeat(-1));
} catch (err) {
console.error(err)
}
}
// ===== Output =====
abcabcabc
00
000
TypeError: String.prototype.repeat called on null or undefined
RangeError: Invalid count value: -1
code:js
// ===== Code =====
{
const paragraph = "I think Ruth's dog is cuter than your dog!";
console.log(paragraph.replace("Ruth's", "my"));
// 予想される結果: "I think my dog is cuter than your dog!"
const regex = /Dog/i;
console.log(paragraph.replace(regex, "ferret"));
// 予想される結果: "I think Ruth's ferret is cuter than your dog!"
// replace / replaceAllの違い
console.log(paragraph.replace("dog", "ferret"));
console.log(paragraph.replaceAll("dog", "ferret"));
// もともとこれでreplaceAllできてはいた
console.log(paragraph.replace(/dog/g, "ferret"));
}
// ===== Output =====
I think my dog is cuter than your dog!
I think Ruth's ferret is cuter than your dog!
I think Ruth's ferret is cuter than your dog!
I think Ruth's ferret is cuter than your ferret!
I think Ruth's ferret is cuter than your ferret!
code:js
// ===== Code =====
// Symbol.replace自前実装パターン
{
const myRegExp = {
return thisValue + replaceValue;
},
}
console.log("I think Ruth's dog is cuter than your dog!".replace(myRegExp, "ferret"));
}
// ===== Output =====
I think Ruth's dog is cuter than your dog!ferret
code:js
// ===== Code =====
// Functional replaceパターン
{
const paragraph = "I think Ruth's dog is cuter than your dog!";
// String.prototype.replaceによるfunctional replacement
console.log(paragraph.replace("dog", (m) => m.toUpperCase()));
console.log(paragraph.replace("dog", (searchValue, position, thisValue) => (
console.log(JSON.stringify({ searchValue, position, thisValue }, null, 2)),
searchValue.toUpperCase()
)));
console.log(paragraph.replace(/dog/g, (m) => m.toUpperCase()));
console.log(paragraph.replace(/dog/g, (searchValue, position, thisValue) => (
console.log(JSON.stringify({ searchValue, position, thisValue }, null, 2)),
searchValue.toUpperCase()
)));
}
// ===== Output =====
I think Ruth's DOG is cuter than your dog!
{
"searchValue": "dog",
"position": 15,
"thisValue": "I think Ruth's dog is cuter than your dog!"
}
I think Ruth's DOG is cuter than your dog!
I think Ruth's DOG is cuter than your DOG!
{
"searchValue": "dog",
"position": 15,
"thisValue": "I think Ruth's dog is cuter than your dog!"
}
{
"searchValue": "dog",
"position": 38,
"thisValue": "I think Ruth's dog is cuter than your dog!"
}
I think Ruth's DOG is cuter than your DOG!