如何从字符串中提取变量子字符串

时间:2017-07-02 16:10:38

标签: javascript regex

我想使用javascript从字符串中提取子字符串,然后用另一个字符替换为字符集。

我知道字符串的前缀和后缀。在前缀和后缀之间是我想要提取的变量子字符串,并替换它上面的字符集。

例如,此字符串表示错误URL。我需要从中提取网址,在此示例中为https%3A//revoked.badssl.com/,然后将%3A替换为:

逻辑是,前缀是:about:neterror?e=nssFailure2&u=,后缀是&c=UTF*,因为我不关心&字符后的其余字符串。

我可能需要使用正则表达式。但是,我知道如何使用Regex来比较字符串是否与特定模式匹配。但我想知道如何使用正则表达式提取子字符串?

about:neterror?e=nssFailure2&u=https%3A//revoked.badssl.com/&c=UTF8&
f=regular d=An%20error%20occurred%20during%20a%20connection%20to%20revoked.badssl.com.
%0A%0APeer%E2%80%99s%20Certificate%20has%20been%20revoked.%0A%0AError%20code
%3A%20%3Ca%20id%3D%22errorCode%22%20title%3D%22SEC_ERROR_REVOKED_CERTIFICATE
%22%3ESEC_ERROR_REVOKED_CERTIFICATE%3C/a%3E%0A

编辑: 当我尝试这个脚本时:

var myString="about:neterror?e=nssFailure2&u=https%3A//abcde.somethig/&c=UTF-8&f=regular&d=An%20error%20occurred%20during%20a%20connection%20to%20abcde.somethig.\
%0A%0ACannot%20communicate%20securely%20with%20peer%3A%20no%20common%20encryption%20algorithm%28s%29.%0A%0AError%20code%3A%20%3Ca%20id%3D%22errorCode%22%20\
title%3D%22SEC_ERROR_REVOKED_CERTIFICATE%22%3ESEC_ERROR_REVOKED_CERTIFICATE%3C/a%3E%0A";
console.log(myString.replace(/^about:neterror\?e=nssFailure2&u=(https)%3A(.*)&c=UTF8.*$/, "$1:$2"));

注意:我在行尾使用\只是为了打破编辑器中的字符串。但它不是原始字符串的一部分。 我的输出与输入相同。

注2: 如果我想使用&作为后缀的策略标记?我使用console.log(myString.replace(/^about:neterror\?e=nssFailure2&u=(https)%3A(.*)&.*$/, "$1:$2"));,但它会打印:https://abcde.somethig/&c=UTF-8&f=regular,我想要:https://abcde.somethig/

3 个答案:

答案 0 :(得分:1)

最简单我可以想到不使用一些复杂的正则表达式并假设& c和& u是静态的,这是 - 首先按照Jedi的建议解码字符串



var str = "about:neterror?e=nssFailure2&u=https%3A//revoked.badssl.com/&c=UTF8&f=regulard=An%20error%20occurred%20during%20a%20connection%20to%20revoked.badssl.com.%0A%0APeer%E2%80%99s%20Certificate%20has%20been%20revoked.%0A%0AError%20code%3A%20%3Ca%20id%3D%22errorCode%22%20title%3D%22SEC_ERROR_REVOKED_CERTIFICATE%22%3ESEC_ERROR_REVOKED_CERTIFICATE%3C/a%3E%0A"

var url = decodeURIComponent(str).split("&u=")[1].split("&c")[0];
console.log(url)




这就是为什么我关闭的原因:



function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

var str = "about:neterror?e=nssFailure2&u=https%3A//revoked.badssl.com/&c=UTF8&f=regulard=An%20error%20occurred%20during%20a%20connection%20to%20revoked.badssl.com.%0A%0APeer%E2%80%99s%20Certificate%20has%20been%20revoked.%0A%0AError%20code%3A%20%3Ca%20id%3D%22errorCode%22%20title%3D%22SEC_ERROR_REVOKED_CERTIFICATE%22%3ESEC_ERROR_REVOKED_CERTIFICATE%3C/a%3E%0A"

var url = getParameterByName("u",str);
console.log(url);




答案 1 :(得分:1)

您要做的是从URL检索查询参数,然后对其进行解码。为此,您应该使用库构建用于此目的,而不是regexp。例如,许多regexp方法将取决于查询参数的精确顺序,这是你不能真正(或不应该)依赖的。

以下是使用URL API的示例,该示例稳定且widely supported(IE11除外)和bug in Firefox用于非http类型的网址,例如about:neterror在被修复的过程中):

var str = "about:neterror?e=nssFailure2&u=https%3A//revoked.badssl.com/&c=UTF8&f=regulard=An%20error%20occurred%20during%20a%20connection%20to%20revoked.badssl.com.%0A%0APeer%E2%80%99s%20Certificate%20has%20been%20revoked.%0A%0AError%20code%3A%20%3Ca%20id%3D%22errorCode%22%20title%3D%22SEC_ERROR_REVOKED_CERTIFICATE%22%3ESEC_ERROR_REVOKED_CERTIFICATE%3C/a%3E%0A";

var url = new URL(str);

console.log(url.searchParams.get('u'));

答案 2 :(得分:0)

您可以捕获所需的子字符串,然后使用反向引用来提取并重新格式化它:



var s = "about:neterror?e=nssFailure2&u=https%3A//revoked.badssl.com/&c=UTF8&f=regular d=An%20error%20occurred%20during%20a%20connection%20to%20revoked.badssl.com.%0A%0APeer%E2%80%99s%20Certificate%20has%20been%20revoked.%0A%0AError%20code%3A%20%3Ca%20id%3D%22errorCode%22%20title%3D%22SEC_ERROR_REVOKED_CERTIFICATE%22%3ESEC_ERROR_REVOKED_CERTIFICATE%3C/a%3E%0A"

var url = s.replace(/^about:neterror\?e=nssFailure2&u=(https)%3A(.*)&c=UTF[\s\S]*$/, "$1:$2")

console.log(url)




使用decodeURIComponent



var s = "about:neterror?e=nssFailure2&u=https%3A//revoked.badssl.com/&c=UTF8&f=regular d=An%20error%20occurred%20during%20a%20connection%20to%20revoked.badssl.com.%0A%0APeer%E2%80%99s%20Certificate%20has%20been%20revoked.%0A%0AError%20code%3A%20%3Ca%20id%3D%22errorCode%22%20title%3D%22SEC_ERROR_REVOKED_CERTIFICATE%22%3ESEC_ERROR_REVOKED_CERTIFICATE%3C/a%3E%0A"

var url = decodeURIComponent(s).replace(/^about:neterror\?e=nssFailure2&u=(.*)&c=UTF[\s\S]*$/, "$1")

console.log(url)