简体   繁体   English

javascript中的正则表达式,检测可选参数

[英]regular expression in javascript, detecting optional arguments

I'm doing replacements on some tokenized strings like below: 我正在对一些标记化字符串进行替换,如下所示:

var myString = "I would like to replace the following token <<start<arg1,arg2 >>> in this string"
OR  
var myString = "The expression has an optional <<start<arg1,arg2,arg3 >>> third argument" 

Within the token, there are 2 or 3 alphanumeric strings which I would like to capture and pass to the regex callback function: 在令牌中,我想捕获并传递给regex回调函数的2或3个字母数字字符串:

myString.replace(regExExpression, function(m, arg1, arg2, arg3) { 
   return foo(arg1, arg2, arg3); 
});

What regex expression will match both versions of myString, capture arg1, arg2, and the optional arg3? 什么正则表达式将匹配myString,捕获arg1,arg2和可选arg3的两个版本?

Description 描述

Try this which will put each arg into a group 1 2 or 3 respectively, see also this regexplanet sample 尝试此操作,将每个arg分别放入1 2或3组, 另请参见此regexplanet示例

  • Each attribute group starts with <<start< and ends with >>> 每个属性组以<<start<开始,以>>>结尾
  • Will match between 2 and 3 groups of comma delimited substrings between the open and close tags, additional groups could be matched by inserting (?:,([^,>]*))? 将在打开和关闭标签2个3之间组逗号分隔的子串之间匹配时,其它基团可以通过将被匹配(?:,([^,>]*))? between the second and third groups. 在第二和第三组之间。
  • Token 3 is optional 令牌3是可选的

<<start<([^,>]*)(?:,([^,>]*))(?:,([^>]*))?(?=>>>)

在此处输入图片说明

I think you just want the regex 我觉得你只是想在正则表达式

/<<(.*?)>>>/g

This should also work if there are multiple samples per line, but not if they are nested. 如果每行有多个样本,那么这也应该起作用,但如果嵌套,则不会。 If that doesn't cover your actual question, it's because it's a bit unclear. 如果那还不能解决您的实际问题,那是因为还不清楚。

I following handles your specific case: 我将处理您的具体情况:

  /<<\s*start\s*<\s*(?:([^,>]*))?(?:\s*,\s*([^,>]*))?(?:\s*,\s*([^>]*))?>>>/i

I usually sprinkle my REs with \\s* to accept free form input. 我通常在RE上撒上\\ s *以接受自由格式输入。 Remove them if you are sure not to need them. 如果确定不需要它们,请将它们删除。 I do not think that you can write a single RE to handle an arbitrary number of arguments but you specified a maximum of 3. 我认为您不能编写单个RE来处理任意数量的参数,但您最多可以指定3个。

My recommendation would be to use the following two stage solution which gives you the extra flexibility of an arbitrary number of arguments : 我的建议是使用以下两个阶段的解决方案,它为您提供任意数量的参数的额外灵活性:

  .replace (/<<\s*start\s*<\s*(.*)?\s*>>>/i, function (m, args) {
     args = args.split (/\s*,\s*/); // array of args
     return  ('{{[' + args.length + '] : ' + args.join (', ') + '}}');
  });

And used as follows : 并使用如下:

"I would like to replace the following token <<start<arg1,arg2 , arg3>>> in this string".replace (/<<\s*start\s*<\s*(.*)?\s*>>>/i, function (m, args) {
         args = args.split (/\s*,\s*/); // arbitrary number of
         return  ('{{[' + args.length + '] : ' + args.join (', ') + '}}');
      });

Resulting in : 导致 :

"I would like to replace the following token {{[3] : arg1, arg2, arg3}} in this string"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM