[英]How to get the url string from a text javascript
如何從生成的文本獲取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
最簡單的方法是使用正則表達式:
disp = text.replace(/(http:\/\/[^ ]+)/, "<a href='$1'>$1</a>");
實際操作見這里: http : //jsfiddle.net/49YFF/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.