描述
此方法用於在將字符串與正則表達式匹配時檢索匹配項。它的工作原理類似於regexp.exec(string)沒有g標誌,它返回一個與所有匹配的數組g旗幟。
用法
下麵給出的是match()JavaScript 的方法。我們可以在 CoffeeScript 代碼中使用相同的方法。
string.match( param )
示例
下麵的例子演示了使用match()CoffeeScript 代碼中的 JavaScript 方法。將此代碼保存在具有名稱的文件中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
打開command prompt並編譯 .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);
現在,打開command prompt再次運行 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' ]
相關用法
- CoffeeScript String slice()用法及代碼示例
- CoffeeScript String toUpperCase()用法及代碼示例
- CoffeeScript String toLocaleUpperCase()用法及代碼示例
- CoffeeScript String concat()用法及代碼示例
- CoffeeScript String lastIndexOf()用法及代碼示例
- CoffeeScript String substr()用法及代碼示例
- CoffeeScript String split()用法及代碼示例
- CoffeeScript String toLowerCase()用法及代碼示例
- CoffeeScript String charAt()用法及代碼示例
- CoffeeScript String search()用法及代碼示例
- CoffeeScript String localeCompare()用法及代碼示例
- CoffeeScript String toLocaleLowerCase()用法及代碼示例
- CoffeeScript String indexOf()用法及代碼示例
- CoffeeScript String charCodeAt()用法及代碼示例
- CoffeeScript Date setHours()用法及代碼示例
- CoffeeScript Date setUTCFullYear()用法及代碼示例
- CoffeeScript Date getDay()用法及代碼示例
- CoffeeScript Date getDate()用法及代碼示例
- CoffeeScript Date getYear()用法及代碼示例
- CoffeeScript Math pow()用法及代碼示例
注:本文由純淨天空篩選整理自 CoffeeScript String - match()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。