Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.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

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 Regexp解析prо;瑕疵_Javascript_Regex_Parsing - Fatal编程技术网

Javascript Regexp解析prо;瑕疵

Javascript Regexp解析prо;瑕疵,javascript,regex,parsing,Javascript,Regex,Parsing,有一个block div,其中包含未知数量的链接,如“a href onclick”,如果有多个链接,则它们用逗号和空格分隔 var reg = /<div class="labeled fl_l"><a href="[^"]*" onclick="[^"]*">(.+?)<\/a>(, <a href="[^"]*" onclick="[^"]*">(.+?)<\/a>{1,})?<\/div>/mg; var arr;

有一个block div,其中包含未知数量的链接,如“a href onclick”,如果有多个链接,则它们用逗号和空格分隔

var reg = /<div class="labeled fl_l"><a href="[^"]*" onclick="[^"]*">(.+?)<\/a>(, <a href="[^"]*" onclick="[^"]*">(.+?)<\/a>{1,})?<\/div>/mg;
var arr;
while ((arr = reg.exec(data)) != null) {
            console.log(arr[0]); //contains the entire text (because it is java script)
    console.log(arr[1]); //contains the name of the first link
    console.log(arr[2]); //contains the following "a href" entirely (if I will point out (?: x, <a... /a>), then the nested brackets will not work)
    console.log(arr[3]); //contains the name of the second link, **and then all of the code**
}
var-reg=/(.+?)(,(.+?){1,})?/mg;
var-arr;
while((arr=reg.exec(data))!=null){
log(arr[0]);//包含整个文本(因为它是java脚本)
console.log(arr[1]);//包含第一个链接的名称
console.log(arr[2]);//完全包含以下“a href”(如果我指出(?:x,),则嵌套的括号将不起作用)
log(arr[3]);//包含第二个链接的名称,**然后是所有代码**
}
}


我认为应该使用
([^如果使用正则表达式对此非常理想(它们不是),我将使用两个单独的表达式,一个用于查找和之间的所有内容,然后另一个用于查找每个链接


但是,你可能想考虑使用XPath来迭代链接。

我马上就要阻止你了。是时候让你知道TURUTH了……除了PLALX的评论之外,如果你要问一个正则表达式的问题,至少给出一些源代码和你希望提取的例子。然后忘记ReGEX并使用DOM MANI。Javascript中专门设计的填充函数。不要用正则表达式解析HTML。你在Javascript中,难道你没有访问DOM元素本身的权限吗?它已经被解析了,你只需要访问它。从
getElementByClass
开始获取父div,然后从
getElementByTagName
开始获取所有
A
标记。然后您可以使用
getAttributeNode
获取您感兴趣的任何属性。