Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.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-获取textarea中的所有URL_Javascript_Jquery_Regex - Fatal编程技术网

Javascript-获取textarea中的所有URL

Javascript-获取textarea中的所有URL,javascript,jquery,regex,Javascript,Jquery,Regex,我正在寻找一个干净的jquery代码,它返回文本区域中的URL。。我已经找到了一些解决办法,但没有那么有用。 假设我有一些文字: 我曾经访问过这里,它对我来说非常有用 巴基斯坦的工作。帮我找到了它 因此,脚本应该在上面的字符串中返回2个URL 到目前为止,我所尝试的是 <script> $(document).ready(function(){ $("#textarea").on('keyup', function(){ const regex = /((?:

我正在寻找一个干净的jquery代码,它返回文本区域中的URL。。我已经找到了一些解决办法,但没有那么有用。 假设我有一些文字:

我曾经访问过这里,它对我来说非常有用 巴基斯坦的工作。帮我找到了它

因此,脚本应该在上面的字符串中返回2个URL

到目前为止,我所尝试的是

<script>
  $(document).ready(function(){

    $("#textarea").on('keyup', function(){

    const regex = /((?:https?:|www\.)[^\s]+)/g;
    const str = $(this).val();
    let m;

    while ((m = regex.exec(str)) !== null) {
        // This is necessary to avoid infinite loops with zero-width matches
        if (m.index === regex.lastIndex) {
            regex.lastIndex++;
        }

        // The result can be accessed through the `m`-variable.
        m.forEach((match, groupIndex) => {
            console.log(`Found match, group ${groupIndex}: ${match}`);
        });
    }

     });

  });
</script>

$(文档).ready(函数(){
$(“#textarea”).on('keyup',function(){
const regex=/((?:https?:| www\)[^\s]+)/g;
const str=$(this.val();
让m;
while((m=regex.exec(str))!==null){
//这是避免具有零宽度匹配的无限循环所必需的
if(m.index==regex.lastIndex){
regex.lastIndex++;
}
//可以通过'm`-变量访问结果。
m、 forEach((匹配,组索引)=>{
log(`Found match,group${groupIndex}:${match}`);
});
}
});
});

可以在
的同时去掉
,并使用
match()

$(“textarea”)。在('keyup',function()上{
const regex=/((?:https?:| www\)[^\s]+)/g;
const str=$(this.val();
设m=str.match(regex);
如果(m){
m、 forEach((匹配,组索引)=>{
log(`Found match,group${groupIndex}:${match}`);
});
}
}).keyup();//演示的触发事件

我去过https://jobscane.com 而且这对找到
巴基斯坦的工作。及https://google.com 帮我找到了它。

可以在
的同时去掉
,并使用
match()

$(“textarea”)。在('keyup',function()上{
const regex=/((?:https?:| www\)[^\s]+)/g;
const str=$(this.val();
设m=str.match(regex);
如果(m){
m、 forEach((匹配,组索引)=>{
log(`Found match,group${groupIndex}:${match}`);
});
}
}).keyup();//演示的触发事件

我去过https://jobscane.com 而且这对找到
巴基斯坦的工作。及https://google.com 帮我找到了它。

只要做
const matches=str.match(regex),代码就可以简化很多matches.forEach上循环(m=>{console.log(
找到匹配:${m}
);})。。。但我不知道你在问什么。这段代码两次返回相同的url。。因此,如果我输入3个URL,它的输出显示6个URL如果我正确读取您的代码,那么您在
keyup
上启动,因此它在“每个”keyup上启动…耶#EdSF!最好的选择是什么?根据您的需要,尝试一下,下面的答案是您应该做的。您的代码可以通过执行
const matches=str.match(regex)简化很多matches.forEach上循环(m=>{console.log(
找到匹配:${m}
);})。。。但我不知道你在问什么。这段代码两次返回相同的url。。因此,如果我输入3个URL,它的输出显示6个URL如果我正确读取您的代码,那么您在
keyup
上启动,因此它在“每个”keyup上启动…耶#EdSF!最好的选择是什么?根据您的需要,尝试一下,下面的答案是您应该做什么。@EdSF不确定您得到了什么at@EdSF哦,是的…不知道Op想要什么。只是用他们的例子。由他们来处理这件事behavior@EdSF不知道你得到了什么at@EdSF哦,是的…不知道Op想要什么。只是用他们的例子。由他们来整理事件行为