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 在js中拆分模式不匹配的字符串?_Javascript_Regex - Fatal编程技术网

Javascript 在js中拆分模式不匹配的字符串?

Javascript 在js中拆分模式不匹配的字符串?,javascript,regex,Javascript,Regex,我正在尝试拆分模式不匹配的TextArea值 案文如下: Some Good Tutorials http://a.com/page1 http://a.com/page2 Some Good Images http://i.com/p1 http://i.com/p2 Some Good Videos http://m.com/p1 http://m.com/p2 现在我只想从文本中获取链接,因此更好的解决方案是将整个字符串拆分为一个字符串数组,其中a行不是url,然后在此数组中用“\n”拆

我正在尝试拆分模式不匹配的TextArea值

案文如下:

Some Good Tutorials
http://a.com/page1
http://a.com/page2
Some Good Images
http://i.com/p1
http://i.com/p2
Some Good Videos
http://m.com/p1
http://m.com/p2
现在我只想从文本中获取链接,因此更好的解决方案是将整个字符串拆分为一个字符串数组,其中a行不是url,然后在此数组中用“\n”拆分每个字符串

编辑:

好的,我找到了一个解决方案,我可以找到不以http://或https://开头的行,然后用一个好的占位符替换它们,这样我就可以得到链接了
虽然我在正则表达式方面很弱,但有人能告诉我如何在javascript中做到这一点吗?

匹配模式。别跟它扯上关系

value=value.match(/http\:\/\/。+/g)


(.+将所有内容匹配到一行末尾)

最终解决!代码如下:

function split_lines() {
    var oText = $('linkTxtArea').value;
    removeBlankLines(); // a helper function to remove blank lines
    oText = oText.split("\n"); // first split the string into an array
    for (i = 0; i < oText.length; i++) // loop over the array
    {
        if (!oText[i].match(/^http:/)) // check to see if the line does not begins with http:
        {
            oText[i] = oText[i].replace(oText[i], "!replaced!"); // replace it with '!replaced!'
        }
    }
    oText = oText.toString().split("!replaced!"); // now join the array to a string and then split that string by '!replaced!'
    for (i = 1; i < oText.length; i++)
    {
        oText[i] = oText[i].replace(/^,/,"").replace(/,$/,""); // there were some extra commas left so i fixed it
    }
    return oText;
}
函数分割线(){
var oText=$('linkTxtArea')。值;
removeBlankLines();//用于删除空行的辅助函数
oText=oText.split(“\n”);//首先将字符串拆分为数组
对于(i=0;i
如果您只需要url,为什么不直接使用match而不是split获取它们:类似于/(http:\/\/.*)/g的内容。根据您的正则表达式引擎是否跨越换行符,您可能需要稍微调整前面的内容,并且可能需要在运行正则表达式之前添加换行符,以确保获得最后一个链接。我不能这样做,因为我希望将单独的链接分组在一起,例如在上面的示例中,一些链接与视频相关,而一些与图像相关。我希望图像的链接单独分组,其他链接单独分组