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

标签 javascript regex

代码:

<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>

我想使用正则表达式获取数组中 [[ ]] 中存在的所有值。请帮助我。

最佳答案

你可以这样做:

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

关于javascript - 使用正则表达式从字符串获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24492234/

相关文章:

javascript - 在 Meteor 中访问 node.js 文件系统模块

javascript - 在离线(无网络)模式下显示 SVG

python - 使用正则表达式 python 匹配文件中的多行

javascript - 对象不支持 ajax POST 到 Web Api 上的属性或方法 'val'

javascript - 无法通过设置 video.currentTime = {value} 来设置 video.currentTime

regex - 使用 UNIX/Linux 操作(排除行).csv 文件的特定列

regex - 使用 sub 增加字符串中的数字

javascript - 如何使用 javascript 验证十进制时间?

regex - 搜索并替换文件中的第 n 次出现

c# - 实现 Flash 网站横幅的最佳方式