Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

javascript - 正则表达式-重复捕获组(Regex - Repeating Capturing Group)

I'm trying to figure out how I can repeat a capture group on the comma-separated values in this the following url string:(我试图弄清楚如何在以下URL字符串中的comma-separated值上重复捕获组:)

id=1,2;name=user1,user2,user3;city=Oakland,San Francisco,Seattle;zip=94553,94523;

I'm using this RegExp which is return results I want, except for the values since they're dynamic ie.(我正在使用此RegExp ,这是我想要的返回结果,但这些值除外,因为它们是动态的,即。)

could be 2,3,4,etc users in the url parameter and was wondering if I could create a capture group for each value instead of user1,user2,user3 as one capture-group.(可能是url参数中的2、3、4等用户,想知道是否可以为每个值创建一个捕获组,而不是将user1,user2,user3作为一个捕获组。)

RegExp: (^|;|:)(\w+)=([^;]+)*(RegExp: (^|;|:)(\w+)=([^;]+)*)

Here is a live demo of it online using RegExp(这是使用RegExp在线在线演示)

Example Output:(示例输出:)

  • Group1 - (semi-colon,colon)(第1组-(分号,分号))
  • Group2 - (key ie. id,name,city,zip)(组2-(密钥,即ID,名称,城市,邮政编码))
  • Group3 - (value1)(组3-(值1))
  • Group4 - (value2) *if exists(组4-(值2)*如果存在)
  • Group5 - (value3) *if exists(组5-(值3)*如果存在)
  • Group6 - (value4) *if exists(组6-(值4)*如果存在)

etc... based on the dynamic values like I explained before.(等等...基于我之前解释的动态值。)

Question: Whats wrong with my expression I'm using the * to loop for repeated patterns?(问题:我的表达式在使用*循环重复模式时出了什么问题?)

  ask by Jordan Davis translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Regex doesn't support what you're trying to do.(正则表达式不支持您要执行的操作。)

When the engine enters the capturing group a second time, it overwrites what it had captured the first time.(当引擎第二次进入捕获组时,它将覆盖第一次捕获的内容。) Consider a simple example (thanks regular-expressions.info ): /(abc|123)+/ used on 'abc123' .(考虑一个简单的示例(感谢regular-expressions.info ): /(abc|123)+/用于'abc123' 。) It will match "abc" then see the plus and try again, matching the "123".(它将匹配“ abc”,然后看到加号,然后重试,匹配“ 123”。) The final capturing group in the output will be "123".(输出中的最终捕获组将为“ 123”。)

This happens no matter what pattern you try and any limitation you set simply changes when the regex will accept the string.(无论您尝试哪种模式,并且您设置的任何限制都会在正则表达式接受字符串时发生更改,都会发生这种情况。)

Consider /(abc|123){2}/ .(考虑/(abc|123){2}/ 。) This accepts 'abc123' with the capturing group as "123" but not 'abc123abc'.(这接受捕获组为“ 123”的“ abc123”,但不接受“ abc123abc”。) Putting a capturing group inside another doesn't work either.(将捕获组放在另一个中也不起作用。) When you create a capturing group, it's like creating a variable.(创建捕获组时,就像创建变量一样。) It can only have one value and subsequent values overwrite the previous one.(它只能有一个值,随后的值会覆盖前一个值。) You'll never be able to have more capturing groups than you have parentheses pairs (you can definitely have fewer, though).(您将永远无法拥有比括号对更多的捕获组(不过绝对可以更少)。)

A possible fix then would be to split the string on ';', then each of those on '=', then the right-hand side of those on ','.(然后,可能的解决方法是将字符串拆分为';',然后将每个拆分为'=',然后将其拆分为右侧的','。)

That would get you [['id', '1', '2'], ['name', 'user1', ...], ['city', ...], ['zip', ...]] .(那会让你[['id', '1', '2'], ['name', 'user1', ...], ['city', ...], ['zip', ...]] 。)

That comes out to be:(结果是:)

function (str) {
  var afterSplit = str.split(';|:');
  afterSplit.pop() // final semicolon creates empty string
  for (var i = 0; i < afterSplit.length; i++) {
    afterSplit[i] = afterSplit[i].split('=');
    afterSplit[i][1] = afterSplit[i][1].split(','); // optionally, you can flatten the array from here to get something nicer
  }
  return afterSplit;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...