May 09, 2021 CoffeeScript
You need to write a complex regular expression.
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']
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 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.