Coffeescript 简明教程
CoffeeScript - Regular Expressions
正则表达式是描述字符模式的对象,JavaScript 支持。在 JavaScript 中,RegExp 类表示正则表达式,String 和 RegExp 都定义了使用正则表达式对文本执行功能强大的模式匹配和搜索并替换功能的方法。
A regular expression is an object that describes a pattern of characters JavaScript supports. In JavaScript, RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on text.
Regular Expressions in CoffeeScript
CoffeeScript 中的正则表达式与 JavaScript 相同。访问以下链接以查看 JavaScript 中的正则表达式− javascript_regular_expressions
The regular expressions in CoffeeScript are same as JavaScript. Visit the following link to see the regular expressions in JavaScript − javascript_regular_expressions
Syntax
通过将 RegExp 模式放在反斜杠之间来定义 CoffeeScript 中的正则表达式,如下所示。
A regular expression in CoffeeScript is defined by placing the RegExp pattern between the forward slashes as shown below.
pattern =/pattern/
Example
以下是 CoffeeScript 中正则表达式的示例。此处,我们创建了一个表达式以查找处于粗体中的数据(<b> 和 </b> 标记之间的数据)。将此代码保存在名为 regex_example.coffee 的文件中
Following is an example of regular expressions in CoffeeScript. In here, we have created an expression that finds out the data that is in bold (data between <b> and </b> tags). Save this code in a file with name regex_example.coffee
input_data ="hello how are you welcome to <b>Tutorials Point.</b>"
regex = /<b>(.*)<\/b>/
result = regex.exec(input_data)
console.log result
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c regex_example.coffee
编译后,它会给你以下 JavaScript。
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var input_data, regex, result;
input_data = "hello how are you welcome to <b>Tutorials Point.</b>";
regex = /<b>(.*)<\/b>/;
result = regex.exec(input_data);
console.log(result);
}).call(this);
现在,再次打开 command prompt 并按照以下所示运行 CoffeeScript 文件。
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee regex_example.coffee
执行后,CoffeeScript 文件产生以下输出。
On executing, the CoffeeScript file produces the following output.
[ '<b>Tutorials Point.</b>',
'Tutorials Point.',
index: 29,
input: 'hello how are you welcome to <b> Tutorials Point.</b>' ]
heregex
我们使用 JavaScript 提供的语法编写的复杂正则表达式不可读,因此,为了使正则表达式更具可读性,CoffeeScript 为正则表达式提供了一个扩展语法,称为 heregex 。使用此语法,我们可以使用空格打断常规的正则表达式,并且我们还可以在这些扩展的正则表达式中使用注释,从而使它们更加用户友好。
The complex regular expressions we write using the syntax provided by JavaScript are unreadable, therefore to make Regular expressions more readable, CoffeeScript provides an extended syntax for regular expressions known as heregex. Using this syntax, we can break the normal regular expressions using whitespaces and we can also use comments in these extended regular expressions, thus making them more user friendly.
Example
以下示例演示了在 CoffeeScript 中使用高级正则表达式 heregex 。此处,我们使用高级正则表达式重写上述示例。将此代码保存在名为 heregex_example.coffee 的文件中
The following example demonstrates the usage of the advanced regular expressions in CoffeeScript heregex. In here, we are rewriting the above example using the advanced regular expressions. Save this code in a file with name heregex_example.coffee
input_data ="hello how are you welcome to Tutorials Point."
heregex = ///
<b> #bold opening tag
(.*) #the tag value
</b> #bold closing tag
///
result = heregex.exec(input_data)
console.log result
打开 command prompt 并按照以下所示编译 .coffee 文件。
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c heregex_example.coffee
编译后,它会给你以下 JavaScript。
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var heregex, input_data, result;
input_data = "hello how are you welcome to <b> Tutorials Point.</b>";
heregex = /<b>(.*) <\/b>/;
result = heregex.exec(input_data);
console.log(result);
}).call(this);
现在,再次打开 command prompt 并按照以下所示运行 CoffeeScript 文件。
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee heregex_example.coffee
执行后,CoffeeScript 文件产生以下输出。
On executing, the CoffeeScript file produces the following output.
[ '<b>Tutorials Point.</b>',
'Tutorials Point.',
index: 29,
input: 'hello how are you welcome to <b>Tutorials Point.</b>' ]