jsで置換え

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>jsで置換え</title>
  6. </head>
  7. <body>
  8. <h1>jsで置換え</h1>
  9. <script type="text/javascript">
  10.   str = "ABCabcdefABCabcABC";
  11.   document.write("オリジナル: str = " + str);
  12.   document.write("<br /><br />");
  13.   document.write("最初に見つかった「abc」を「ooo」にする:<br />");
  14.   document.write("内容: " + 'str.replace("abc", "ooo")' + "<br />");
  15.   document.write("結果: " + str.replace("abc", "ooo") + "<br />");
  16.   document.write("<br />");
  17.   document.write("すべての「abc」を「ooo」にする:<br />");
  18.   document.write("内容: " + 'replaceAll(str, "abc", "ooo")' + " ※replaceAll()は別途宣言。<br />");
  19.   document.write("結果: " + replaceAll(str, "abc", "ooo") + "<br />");
  20.   document.write("<br />");
  21.   document.write("正規表現を使って最初に見つかった「abc」を「ooo」にする:<br />");
  22.   document.write("内容: " + 'str.replace(/abc/, "ooo")' + "<br />");
  23.   document.write("結果: " + str.replace(/abc/, "ooo") + "<br />");
  24.   document.write("<br />");
  25.   document.write("正規表現を使って最初に見つかった「abc(大文字・小文字区別なし)」を「ooo」にする:<br />");
  26.   document.write("内容: " + 'str.replace(/abc/i, "ooo")' + "<br />");
  27.   document.write("結果: " + str.replace(/abc/i, "ooo") + "<br />");
  28.   document.write("<br />");
  29.   document.write("正規表現を使ってすべての「abc」を「ooo」にする:<br />");
  30.   document.write("内容: " + 'strstr.replace(/abc/g, "ooo")' + "<br />");
  31.   document.write("結果: " + str.replace(/abc/g, "ooo") + "<br />");
  32.   document.write("<br />");
  33.   document.write("正規表現を使ってすべての「abc(大文字・小文字区別なし)」を「ooo」にする:<br />");
  34.   document.write("内容: " + 'str.replace(/abc/gi, "ooo")' + "<br />");
  35.   document.write("結果: " + str.replace(/abc/gi, "ooo") + "<br />");
  36.   document.write("<br />");
  37.   document.write("<hr>");
  38.   repStr = "abc";
  39.   document.write("変数を使う:<br />");
  40.   document.write("変数のオリジナル: repStr = " + repStr);
  41.   document.write("<br /><br />");
  42.   
  43.   document.write("変数に「abc」をいれて対象すべてを「ooo」にする:<br />");
  44.   document.write("内容: " + 'str.replace(new RegExp(repStr, "g"), "ooo")' + "<br />");
  45.   document.write("結果: " + str.replace(new RegExp(repStr, "g"), "ooo") + "<br />");
  46.   document.write("<br />");
  47.   document.write("変数に「abc」をいれて対象すべて(大文字・小文字区別なし)を「ooo」にする:<br />");
  48.   document.write("内容: " + 'str.replace(new RegExp(repStr, "gi"), "ooo")' + "<br />");
  49.   document.write("結果: " + str.replace(new RegExp(repStr, "gi"), "ooo") + "<br />");
  50.   document.write("<br />");
  51.   // 対象の文字列を全部置換え
  52.   function replaceAll(str, findStr, replaceStr)
  53.   {
  54.     return str.split(findStr).join(replaceStr);
  55.   }
  56. </script>
  57. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  58. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  59. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  60. </div>
  61. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  62. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  63. </div>
  64. </body>
  65. </html>