Created
August 30, 2017 09:41
-
-
Save 92hackers/7808d95700f898ff91678724d3ed5355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// regex 分组的一个好例子 | |
const pattern = /^(0*([1-9]\d{1,3})\ +)?([1-9]\d{7,})$/ | |
const str = '086 18118771840' | |
pattern.exec(str) | |
/* => [ '086 18118771840', '086 ', '86', '18118771840' ] | |
* 这里,用正则来匹配想要的字段,加上括号,即可自动分组, | |
* 应用: 根据条件提取字段,分组甚至能够在正则表达式里面直接使用。 | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment