Coffeescript-string-match
提供:Dev Guides
CoffeeScript文字列-match()
説明
このメソッドは、文字列を正規表現と照合するときに一致を取得するために使用されます。 これは、 g フラグのない* regexp.exec(string)と同様に機能し、 *g フラグと一致するすべての配列を返します。
構文
以下に、JavaScriptの* match()*メソッドの構文を示します。 CoffeeScriptコードで同じメソッドを使用できます。
string.match( param )
例
次の例は、CoffeeScriptコードでJavaScriptの* match()メソッドを使用する方法を示しています。 このコードを *string_localecompare.coffee という名前のファイルに保存します
str = "For more information, see Chapter 3.4.5.1";
re =/(chapter \d+(\.\d)*)/i;
found = str.match re
console.log found
- コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c coffee string_match.coffee
コンパイル時に、次のJavaScriptが提供されます。
//Generated by CoffeeScript 1.10.0
(function() {
var found, re, str;
str = "For more information, see Chapter 3.4.5.1";
re =/(chapter \d+(\.\d)*)/i;
found = str.match(re);
console.log(found);
}).call(this);
次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。
c:\> coffee string_match.coffee
CoffeeScriptファイルを実行すると、次の出力が生成されます。
[ 'Chapter 3.4.5.1',
'Chapter 3.4.5.1',
'.1',
index: 26,
input: 'For more information, see Chapter 3.4.5.1' ]