Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.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 我怎样才能排除“;[“和”和“];在一场比赛中,比如;[abc]“什么;?_Javascript_Regex - Fatal编程技术网

Javascript 我怎样才能排除“;[“和”和“];在一场比赛中,比如;[abc]“什么;?

Javascript 我怎样才能排除“;[“和”和“];在一场比赛中,比如;[abc]“什么;?,javascript,regex,Javascript,Regex,我有以下字符串: [a] [abc] test [zzzz] 我试图得到这样一个数组: [0] => a [1] => abc [2] => zzzz 我尝试了以下代码: var string = '[a] [abc] test [zzzz]'; var matches = string.match(/\[(.*?)\]/g); for(var i = 0; i < matches.length; i++) console.log(matches[i]);

我有以下字符串:

[a] [abc] test [zzzz]
我试图得到这样一个数组:

[0] => a
[1] => abc
[2] => zzzz
我尝试了以下代码:

var string = '[a] [abc] test [zzzz]';
var matches = string.match(/\[(.*?)\]/g);
for(var i = 0; i < matches.length; i++)
    console.log(matches[i]);
我尝试添加两个非捕获组(
?:
),如下所示:

var matches = string.match(/(?:\[)(.*?)(?:\])/g);
但我看到的是相同的匹配,没有变化


出了什么问题,如何获取所需的阵列?

match
不会捕获全局匹配中的组。为此我做了一个小帮手

String.prototype.gmatch = function(regex) {
  var result = [];
  this.replace(regex, function() {
    var matches = [].slice.call(arguments,1,-2);
    result.push.apply(result, matches);
  });
  return result;
};
然后像这样使用它:

var matches = string.gmatch(/\[(.*?)\])/g);
正则表达式
[[]\s*(\b[^]]*\b)\s*[\]]

组1将包含一个包含所有文本的字符串数组,位于开括号和关括号之间

仅使用正则表达式解决方案的示例 Match将拉出所有匹配的子字符串并显示它们,因为它只使用正则表达式,所以运行速度比使用slice快一些

  var re = /[[]\s*(\b[^]]*\b)\s*[\]]/;
  var sourcestring = "source string to match with pattern";
  var results = [];
  var i = 0;
  for (var matches = re.exec(sourcestring); matches != null; matches = re.exec(sourcestring)) {
    results[i] = matches;
    for (var j=0; j<matches.length; j++) {
      alert("results["+i+"]["+j+"] = " + results[i][j]);
    }
    i++;
  }


(
    [0] => Array
        (
            [0] => [a]
            [1] => [abc]
            [2] => [zzzz]
        )

    [1] => Array
        (
            [0] => a
            [1] => abc
            [2] => zzzz
        )

)
var re=/[[]\s*(\b[^]]*\b)\s*[\]]/;
var sourcestring=“要与模式匹配的源字符串”;
var结果=[];
var i=0;
for(var matches=re.exec(sourcestring);matches!=null;matches=re.exec(sourcestring)){
结果[i]=匹配项;
对于(var j=0;j数组)
(
[0]=>[a]
[1] =>[abc]
[2] =>[zzzz]
)
[1] =>阵列
(
[0]=>a
[1] =>abc
[2] =>zzzz
)
)

你的思路是对的。[(.*)]将匹配你正在寻找的标记,但这些标记有方括号。以后只需删除方括号。这就是为什么JavaScript不支持lookbehinds的原因。
/(?@Kolink未捕获表达式在我看来更好。
(?:\[{1}(?.*\]{1})
@Phill可能吧,但他们不使用
/g
。还有,那
{1}
到底是关于什么的?@Kolink
  var re = /[[]\s*(\b[^]]*\b)\s*[\]]/;
  var sourcestring = "source string to match with pattern";
  var results = [];
  var i = 0;
  for (var matches = re.exec(sourcestring); matches != null; matches = re.exec(sourcestring)) {
    results[i] = matches;
    for (var j=0; j<matches.length; j++) {
      alert("results["+i+"]["+j+"] = " + results[i][j]);
    }
    i++;
  }


(
    [0] => Array
        (
            [0] => [a]
            [1] => [abc]
            [2] => [zzzz]
        )

    [1] => Array
        (
            [0] => a
            [1] => abc
            [2] => zzzz
        )

)