【问题标题】:How to get the url string from a text javascript如何从文本javascript中获取url字符串
【发布时间】:2012-02-11 23:46:45
【问题描述】:

如何从生成的文本中获取 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>";

结果

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

【问题讨论】:

    标签: javascript url hyperlink href


    【解决方案1】:

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

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

    在此处查看实际操作:http://jsfiddle.net/49YFF/

    【讨论】:

    • 太棒了!但我怎样才能得到第一个文本? “这是我的示例文本尝试”?很抱歉给你一个简单的问题,但我只是 Javascript 的新手。
    • @RobinCarloCatacutan - 你点击了我发布的那个链接吗?
    • 刷新之前我没看到。 Tnx 必须尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 2010-11-28
    • 2015-09-24
    • 2015-08-06
    相关资源
    最近更新 更多