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
The regular expressions in CoffeeScript are same as JavaScript. Visit the following link to see the regular expressions in JavaScript − javascript_regular_expressionsSyntax
A regular expression in CoffeeScript is defined by placing the RegExp pattern between the forward slashes as shown below.pattern =/pattern/
Example
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.coffeeinput_data ="hello how are you welcome to <b>Tutorials Point.</b>" regex = /<b>(.*)<\/b>/ result = regex.exec(input_data) console.log resultOpen the command prompt and compile the .coffee file as shown below.
c:\> coffee -c regex_example.coffeeOn 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);Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee regex_example.coffeeOn 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
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
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.coffeeinput_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 resultOpen the command prompt and compile the .coffee file as shown below.
c:\> coffee -c heregex_example.coffeeOn 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);Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee heregex_example.coffeeOn 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>' ]
No comments:
Post a Comment