Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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 REGEX:如何解析同一前缀文本中每个数据点的可变数量_Javascript_Regex - Fatal编程技术网

Javascript REGEX:如何解析同一前缀文本中每个数据点的可变数量

Javascript REGEX:如何解析同一前缀文本中每个数据点的可变数量,javascript,regex,Javascript,Regex,我有这些原始数据 [gallery] Title [galleryitem]http://www.google.com/image.jpg[/galleryitem] [galleryitem]http://www.google.com/image.jpg[/galleryitem] [galleryitem]http://www.google.com/image.jpg[/galleryitem] [galleryitem]http://www.google.com/image.jpg[/ga

我有这些原始数据

[gallery] Title [galleryitem]http://www.google.com/image.jpg[/galleryitem] [galleryitem]http://www.google.com/image.jpg[/galleryitem] [galleryitem]http://www.google.com/image.jpg[/galleryitem] [galleryitem]http://www.google.com/image.jpg[/galleryitem] [galleryitem]http://www.google.com/image.jpg[/galleryitem] [galleryitem]http://www.google.com/image.jpg[/galleryitem] [/gallery] [画廊] 标题 [厨房项目]http://www.google.com/image.jpg[/galleryitem] [厨房项目]http://www.google.com/image.jpg[/galleryitem] [厨房项目]http://www.google.com/image.jpg[/galleryitem] [厨房项目]http://www.google.com/image.jpg[/galleryitem] [厨房项目]http://www.google.com/image.jpg[/galleryitem] [厨房项目]http://www.google.com/image.jpg[/galleryitem] [/画廊] 这是一个图库框,它以图库框的标题开头,后跟一系列封装图像URL的[galleryitem]标记

问题是这些galleryitem图像中会有一个变量,所以我想不出通过正则表达式处理这个问题的方法


理想情况下,我要做的是将所有图像匹配到某种数组中,这样我就可以逐个循环处理。这一切都是在Javascript中完成的。

您需要一次提取一个匹配项。以下是我通常使用的模式:

var bbcode = "...";
var pattern = /\[galleryitem\]([^\[]*)\[\/galleryitem\]/g;
var match, url;

while (match = pattern.exec(bbcode)) {
    url = match[1];
    // do something with url
}

这是怎么回事?RegExp对象(
pattern
)跟踪字符串中的当前索引,每次调用
exec
,它都会从存储的索引开始搜索下一个匹配项。当没有匹配项时,
exec
返回null,循环终止。

方法是反复调用
RegExp.exec()

function getGalleryItems(str) {
  var matches    = [],
    galleryitems = [],
    re           = /\[galleryitem\]([^\[]*)\[\/galleryitem]/g;
  while (matches !== null) {
    matches = re.exec(str);
    if (matches !=== null) {
      galleryitems.push(matches[1]);
    }
  }
  return galleryitems;
}
我仍然(显然…)不熟悉正则表达式,但我想到了以下几点:

var text = "[gallery]Title[galleryitem]http://www.google.com/image.jpg[/galleryitem][galleryitem]http://www.google.com/image.jpg[/galleryitem][galleryitem]http://www.google.com/image.jpg[/galleryitem][galleryitem]http://www.google.com/image.jpg[/galleryitem][galleryitem]http://www.google.com/image.jpg[/galleryitem][galleryitem]http://www.google.com/image.jpg[/galleryitem][/gallery]"​​​​​​​​​​​​​​​​​​​​​​​;

​var parsedText = text.replace(/(\[\/*galleryitem\])|(\[\/*gallery\])|title/gi,' ').split(/\s+/);

var imgs = [];
for (var i=0,len=parsedText.length;i<len;i++){
    if (parsedText[i] != ''){
        imgs.push(parsedText[i]);
    }
}

console.log(imgs);
var text=“[gallery]标题[galleryitem]http://www.google.com/image.jpg[galleryitem][galleryitem]http://www.google.com/image.jpg[galleryitem][galleryitem]http://www.google.com/image.jpg[galleryitem][galleryitem]http://www.google.com/image.jpg[galleryitem][galleryitem]http://www.google.com/image.jpg[galleryitem][galleryitem]http://www.google.com/image.jpg[/galleryitem][/gallery]”​​​​​​​​​​​​​​​​​​​​​​​;
​var parsedText=text.replace(/(\[\/*galleryitem\]))|(\[\/*gallery\]))| title/gi',).split(/\s+/);
var-imgs=[];
对于(var i=0,len=parsedText.length;i