Javascript JS:在文本中查找URL,创建链接
下面用JSBE重写的PHP代码是什么,这样文本块中的url链接就可以替换为html链接?我已经开始了一个新的工作Javascript JS:在文本中查找URL,创建链接,javascript,angularjs,Javascript,Angularjs,下面用JSBE重写的PHP代码是什么,这样文本块中的url链接就可以替换为html链接?我已经开始了一个新的工作 使用以下方法: var text = "The text you want to filter goes here. http://google.com"; text = text.replace( /((http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?)/g, '<a href
使用以下方法:
var text = "The text you want to filter goes here. http://google.com";
text = text.replace(
/((http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?)/g,
'<a href="$1">$1</a>'
);
var text=“要筛选的文本位于此处。http://google.com";
text=text.replace(
/((http | https | ftp | ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?)/g,
''
);
说明:
g
这里是一个全局标志;它将替换所有URL
我还为整个表达式添加了一个捕获组,因此可以使用backreference$1
var text = "The text you want to filter goes here. http://google.com";
text = text.replace(
/((http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?)/g,
'<a href="$1">$1</a>'
);