@@ -200,15 +200,15 @@ console.log(["Apple", "Banana", "Orange"].join("/")); // Apple/Banana/Orange
200200[ ` String#replace ` ] ( https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/replace ) メソッドは、第一引数に与えられた文字列 (または[ 正規表現] ( https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp ) ) に一致する文字列を第二引数の文字列に置き換えた新しい文字列を返します。
201201第一引数が文字列の場合は、最初に一致した場所のみを置き換えます。
202202一致するすべての場所を置き換えたい場合は、代わりに [ ` String#replaceAll ` ] ( https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll ) を使います。
203- どちらのメソッドも、元の文字列は変更されません。
204203
205204``` js
206205const base = " ABCBCC" ;
207206document .write (base .replace (" BC" , " EFG" )); // AEFGBCC
208207document .write (base .replaceAll (" C" , " T" )); // ABTBTT
208+ document .write (base); // ABCBCC (元の文字列は変更されない)
209209```
210210
211- これと ` fs.readFileSync ` 関数を用いることで、HTML を別のファイルに分けて保存することができるようになり、より簡単に管理することができます。
211+ ` String#replace ` と ` fs.readFileSync ` 関数を用いることで、HTML を別のファイルに分けて保存することができるようになり、より簡単に管理することができます。
212212
213213``` html title="index.html"
214214<!doctype html>
@@ -233,9 +233,9 @@ const app = express();
233233
234234const names = [" 田中" , " 鈴木" , " 佐藤" ];
235235app .get (" /" , (request , response ) => {
236- const index = fs .readFileSync (" ./index.html" , " utf-8" );
236+ const template = fs .readFileSync (" ./index.html" , " utf-8" );
237237 const namesAsHTML = names .map ((user ) => ` <li>${ user} </li>` ).join (" " );
238- const html = index .replace (" {users}" , namesAsHTML);
238+ const html = template .replace (" {users}" , namesAsHTML);
239239 response .send (html);
240240});
241241
@@ -316,4 +316,3 @@ app.listen(3000);
316316解答例 2:
317317
318318<ViewSource url = { import .meta .url } path = " _samples/server-or-client" />
319- ````
0 commit comments