Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.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 用锚标记替换http链接_Javascript_Regex - Fatal编程技术网

Javascript 用锚标记替换http链接

Javascript 用锚标记替换http链接,javascript,regex,Javascript,Regex,因此,我正在构建一个聊天客户端,通过json读取传入的消息 但是,我希望能够将URL转换为可单击的锚标记。我环顾了一下四周,但还没有找到任何关于这个问题的帖子 这是我当前的代码: if (json.type === 'message') { var val; var str = json.data.text; //message-string var here = str.match("([^\s]+)"); // match any word until spac

因此,我正在构建一个聊天客户端,通过json读取传入的消息

但是,我希望能够将URL转换为可单击的锚标记。我环顾了一下四周,但还没有找到任何关于这个问题的帖子

这是我当前的代码:

if (json.type === 'message') {
    var val;  
    var str = json.data.text; //message-string
    var here  = str.match("([^\s]+)"); // match any word until space <---| merge
if (!here.search(/^http[s]?:\/\//)){ //if it contains http / https   <---| these?
    val = '<a href=\"' + here + "\"> [Link] </a>";        
}
    addMessage(json.data.author, val, json.data.color, new Date(json.data.time));
}
else {
    console.log('Hmm..., I\'ve never seen JSON like this:', json);
}
if(json.type=='message'){
var-val;
var str=json.data.text;//消息字符串

var here=str.match(([^\s]+)”;//匹配任何单词直到空格您从未使用过
val
,但立即使用
replace
可能会容易得多。您可能需要添加
target=“\u blank”
以确保链接在新窗口中打开,而不是替换聊天页面
由于沙盒的原因,在嵌入式代码段中似乎不起作用,但它在普通页面上起作用)

const text=”这是一条带有链接的消息https://www.google.com 和另一个链接到https://stackoverflow.com';
const changedText=text.replace(
/(https?:\/\/)([^]+)/g,
''
);
console.log(changedText);
document.body.innerHTML+=changedText;