Prototype-string-sub

提供:Dev Guides
2020年6月23日 (火) 02:07時点におけるMaintenance script (トーク | 投稿記録)による版 (Imported from text file)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先:案内検索

プロトタイプ-sub()メソッド

このメソッドは、パターンの最初のカウントオカレンスを通常の文字列、関数の戻り値、またはテンプレート文字列に置き換えた文字列を返します。 パターンは、文字列または正規表現です。

構文

string.sub(pattern, replacement[, count = 1]);

戻り値

文字列を返します。

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>

      <script>
         function showResult() {
            var fruits = 'apple pear orange';
            alert("fruits.sub(' ', ', '):"+  fruits.sub(' ', ', '));
            alert("fruits.sub(' ', ', ', 1): " +  fruits.sub(' ', ', ', 1));
            alert("fruits.sub(' ', ', ', 2): " +  fruits.sub(' ', ', ', 2));
            var str = fruits.sub(/\w+/,
            function(match) { return match[0].capitalize() + ','}, 2);
            alert(str);

         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      <br/>
      <br/>
      <input type = "button" value = "Result" onclick = "showResult();"/>
   </body>
</html>

出力