如何从文本javascript获取url字符串

时间:2012-01-17 05:30:55

标签: javascript url hyperlink href

如何从生成的文本中获取url字符串。 例如,我生成了一个文本

text =  "This is my sample text try http:\/\/sample.com\/23411";

convertedString = "This is my sample text try http://sample.com/23411";

我想这样做

var disp = "";

disp += "This is my sample text try";
disp += "<a href = 'http://sample.com/23411'>http://sample.com/23411</a>";

结果

这是我的示例文本try http://sample.com/23411

1 个答案:

答案 0 :(得分:3)

最简单的方法是使用正则表达式:

disp = text.replace(/(http:\/\/[^ ]+)/, "<a href='$1'>$1</a>");

在此处查看:[{3}}