-1

我想从如下所示的字符串中删除一个 url:

Guardian #Tech #News:“Hillsborough 报告:数据新闻可以告诉我们关于文件的哪些信息” http://t.co/kplfDhLE

4

3 回答 3

0

您可以使用正则表达式来挑选网址:

var reg = /(\bhttps?:\/\/[\w|\d|\.|-|\/]+\b)/gi,
    result = reg.exec(tweet)[0];

http://jsfiddle.net/4rtEb/

于 2012-09-12T17:19:06.720 回答
0
var url = str.match(/\bhttps?:[^\s]+\b/i)[0];
于 2012-09-12T17:20:38.623 回答
0

RegEx 是一种处理字符串的方法,只是不是我喜欢的。如果你也有同样的情况,那么试试这个:

var text = 'Guardian #Tech #News: ...snipped... http://t.co/kplfDhLE';
text.substring(0, text.indexOf("http"));    // Gives only the text
text.substring(text.indexOf("http"));       // Gives only the URL

如果 URL 不在字符串的末尾,那么它会有点棘手,但仍然比使用正则表达式杀死自己要好得多。

于 2012-09-12T17:27:07.853 回答