Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用正则表达式从字符串获取数组_Javascript_Regex - Fatal编程技术网

Javascript 使用正则表达式从字符串获取数组

Javascript 使用正则表达式从字符串获取数组,javascript,regex,Javascript,Regex,代码: 变量re=/^\[.+$/; var str='[[FirstName][[LastName]]观看了“[[$Title]]”并向您发送了以下内容 评论和链接:[[iaf_消息/5]][[if iaf_消息/6] 否]]http://webcasts.advanstar.com/acc/iaf/1/[$PROGRAMID]]/单击 上面的URL将带您进入网络广播:“[$Title]]” [else]]http://webcasts.advanstar.com/acc/iaf/1/[$P

代码:


变量re=/^\[.+$/;
var str='[[FirstName][[LastName]]观看了“[[$Title]]”并向您发送了以下内容
评论和链接:[[iaf_消息/5]][[if iaf_消息/6]
否]]http://webcasts.advanstar.com/acc/iaf/1/[$PROGRAMID]]/单击
上面的URL将带您进入网络广播:“[$Title]]”
[else]]http://webcasts.advanstar.com/acc/iaf/1/[$PROGRAMID]]/?
_IAFSegment=[[$SEGMENT]]和_IAFTime=[[[$posmmss]]单击上面的URL将
在程序中的某个点,带您观看网络广播“[[$Title]]”
[/User/FirstName]]我认为这与你最相关。
来自Advantstar Communications的[[#endif]]消息[发送方IP:[[$IP]]]If
您认为您收到此电子邮件是错误的,或者是因为客户的原因
Accela Communications的服务,请发送消息至
mailto:support.getinfoadvanstar.com';
var myArray=str.match(re);
log(myArray);
我想使用正则表达式获取数组中[[]中存在的所有值。请帮助我。

您可以执行以下操作:

<script language="javascript1.2">
   var re = /^\[\[.+$/;
   var str = '[[FirstName]] [[LastName]] watched "[[$Title]]" and sent you the following               
             comment and link:[[iaf_message/5]][[#if iaf_message/6 
              No]]http://webcasts.advanstar.com/acc/iaf/1/[[$PROGRAMID]]/Clicking the 
             URL above will take you to the webcast: "[[$Title]]" 
             [[#else]]http://webcasts.advanstar.com/acc/iaf/1/[[$PROGRAMID]]/?
              _IAFSegment=[[$SEGMENT]]&_IAFTime=[[$posmmss]]Clicking the URL above will 
              take you to the webcast "[[$Title]]" at the point in the program that 
              [[/User/FirstName]] thought would be most relevant to you.
              [[#endif]]Message from Advanstar Communications[Sender IP: [[$IP]]] If 
              you believe you have received this email in error, or for customer 
              service at Accela Communications, please send a message to 
              mailto:support.getinfoadvanstar.com';

  var myArray = str.match(re);
  console.log(myArray);
</script>

这应该适用于所有版本的JavaScript:

var myArray = str.match(/\[\[.+?\]\]/g).map(
    function(x){ return x.replace(/\[|\]/g,"");
});

请按原样复制并粘贴代码。@Shanmugam很高兴能提供帮助。@Shanmugam不要忘记单击勾号将其标记为答案。如果您不能使用.map,请使用jquery或下划线等提供.map的库。然而,现在大多数支持HTML5的浏览器都支持.map,尽管许多公司都安装了windows XP并在中使用ternet explorer 8,因此没有默认的.map函数。
var mystring = "[[foo]] .. [[bar]] ... [[baz]]";
var pattern = /\[\[([^\]]*)\]\]/g
var array = [];
match = pattern.exec(mystring);
while (match !== null) {
  array.push(match[1]);
  match = pattern.exec(mystring);
}