Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CoffeeScript uses Heregexes


May 09, 2021 CoffeeScript


Table of contents


Use Heregexes

Problem

You need to write a complex regular expression.

Solution

Use CoffeeScript's "heregexes" -- extended regular expressions that ignore internal white space characters and can contain comments.

pattern = ///
  ^\(?(\d{3})\)? # 采集区域代码,忽略可选的括号
  [-\s]?(\d{3})  # 采集前缀,忽略可选破折号或空格
  -?(\d{4})      # 采集行号,忽略可选破折号
///
[area_code, prefix, line] = "(555)123-4567".match(pattern)[1..3]
# => ['555', '123', '4567']

Discuss

By breaking down complex regular expressions and comment focus sections, they become easier to identify and maintain. For example, it is now quite obvious to change regular expressions to allow optional space between prefixes and line numbers.

Blank characters in heregexes

Blank characters are overlooked in heregexes - so what should you do if you want to match characters for ASCII spaces?

Our solution is to s@ character groups that match spaces, tabs, and line breaks. If you only want to match a space, you need to use the word "X20" to represent the literal ASCII space.