且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何提取“零件"根据某些 RegExp 模式的字符串?

更新时间:2023-11-27 08:41:04

使用捕获组:

var res1 = '/this/12/that/34'.match(/\/this\/([0-9a-zA-Z]+)\/that\/([0-9a-zA-Z]+)/);

你会得到一个包含 3 个元素的数组:

You will get an array containing 3 elements:

  1. 整场比赛;

  1. The whole match;

12

34

并且您可以使用 .slice(1) 删除第一个元素:

And you can use .slice(1) to remove the first element:

var res1 = '/this/12/that/34'.match(/\/this\/([0-9a-zA-Z]+)\/that\/([0-9a-zA-Z]+)/).slice(1);