【问题标题】:Javascript regular expression to add protocol to url stringJavascript正则表达式将协议添加到url字符串
【发布时间】:2013-08-05 20:24:55
【问题描述】:

我有一个使用 JavaScript 列出一些网站详细信息的应用程序。将有一个使用 JavaScript 本身生成的网站链接。有时我会得到我的链接,

<a href="http://www.google.com">Website</a>

但有时会,

<a href="www.yahoo.com">Website</a>

第二次链接不通,没有协议。

所以我正在寻找一个 JavaScript 正则表达式函数来添加 http:// 如果没有协议。

我的代码看起来像,

var website_link = document.createElement("a"); 
website_link.innerHTML = "Website"; 
website_link.href = my_JSON_object.website;
website_link.target = "_blank"; 
profile.appendChild(website_link); 

并且不会出现本地链接。

【问题讨论】:

  • ^www.替换为 ^www ?虽然有些子域网址没有 www :)
  • 您尝试解决问题为时已晚。无法判断“www.yahoo.com”是本地文件(可能丢失)还是错误的 URL。

标签: javascript regex url


【解决方案1】:

请参阅this 链接。

function setHttp(link) {
    if (link.search(/^http[s]?\:\/\//) == -1) {
        link = 'http://' + link;
    }
    return link;
}
alert(setHttp("www.google.com"));
alert(setHttp("http://www.google.com/"));  

在你的代码中它会是这样的:

var website_link = document.createElement("a"); 
website_link.innerHTML = "Website";
if (my_JSON_object.website.search(/^http[s]?\:\/\//) == -1) {
    my_JSON_object.website = 'http://' + my_JSON_object.website;
}
website_link.href = my_JSON_object.website;
website_link.target = "_blank"; 
profile.appendChild(website_link); 

【讨论】:

    【解决方案2】:

    例如,使用negative lookahead:

    your_string.replace(/href="(?!http)/, 'href="http://');
    

    例子:

    > '<a href="www.yahoo.com">Website</a>'.replace(/href="(?!http)/, 'href="http://');
    "<a href="http://www.yahoo.com">Website</a>"
    > '<a href="http://www.yahoo.com">Website</a>'.replace(/href="(?!http)/, 'href="http://');
    "<a href="http://www.yahoo.com">Website</a>"
    

    【讨论】:

    • There will be a link to website which is generated using JavaScript itself. 从这里我得到 - 没有本地链接
    • 我的代码看起来像, var website_link = document.createElement("a"); website_link.innerHTML = "网站"; website_link.href = my_JSON_object.website website_link.target = "_blank"; profile.appendChild(website_link);并且没有本地链接。
    • 我看不到您在哪里尝试代码。您需要将replace 命令应用于my_JSON_object.website
    【解决方案3】:

    我已将此功能包装到 NPM 模块 url-schemify

    var schemify = require('url-schemify');
    var assert = require('assert');
    
    // url-schemify adds default scheme (http) to the URLs that miss it
    
    assert.equal(schemify('google.com'), 'http://google.com');
    assert.equal(schemify('www.example.com'), 'http://www.example.com');
    
    // default scheme could be configured through the options parameter
    
    assert.equal(schemify('google.com', { scheme: 'https' }), 'https://google.com');
    // { scheme: '' } will produce protocol-related URL
    assert.equal(schemify('www.example.com', { scheme: '' }), '//www.example.com');
    
    // url-schemify doesn't modify URLs that already have scheme or protocol-related ones:
    
    assert.equal(schemify('http://google.com'), 'http://google.com');
    assert.equal(schemify('https://www.example.com'), 'https://www.example.com');
    assert.equal(schemify('ftp://example.com'), 'ftp://example.com');
    assert.equal(schemify('//example.com'), '//example.com');
    

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 2019-09-27
      • 2021-10-01
      • 1970-01-01
      • 2017-08-01
      • 2011-11-25
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多