Skip to content

Instantly share code, notes, and snippets.

@92hackers
Created August 30, 2017 09:41
Show Gist options
  • Save 92hackers/7808d95700f898ff91678724d3ed5355 to your computer and use it in GitHub Desktop.
Save 92hackers/7808d95700f898ff91678724d3ed5355 to your computer and use it in GitHub Desktop.
// 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