Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于某个前缀从字符串中获取子字符串_Javascript - Fatal编程技术网

Javascript 基于某个前缀从字符串中获取子字符串

Javascript 基于某个前缀从字符串中获取子字符串,javascript,Javascript,我有下面的字符串响应,我存储在一个变量中 TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b, 我想提取一个特定字符串的响应,并将其存储在一个单独的变量中进行打印 例如。。如果必须提取缓存控

我有下面的字符串响应,我存储在一个变量中

TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,
我想提取一个特定字符串的响应,并将其存储在一个单独的变量中进行打印

例如。。如果必须提取缓存控制的响应-max age=900,public

尝试了split、indexof和其他一些方法,但没有任何效果。

使用正则表达式匹配:

var Response = "TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,",
    CacheControlRegEx = /Cache-Control:\s(.+?),/,
    CacheControl = false;

if (CacheControl = CacheControlRegEx.exec(Response)){
    CacheControl = CacheControl[1];
}
使用正则表达式匹配:

var Response = "TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,",
    CacheControlRegEx = /Cache-Control:\s(.+?),/,
    CacheControl = false;

if (CacheControl = CacheControlRegEx.exec(Response)){
    CacheControl = CacheControl[1];
}
使用正则表达式匹配:

var Response = "TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,",
    CacheControlRegEx = /Cache-Control:\s(.+?),/,
    CacheControl = false;

if (CacheControl = CacheControlRegEx.exec(Response)){
    CacheControl = CacheControl[1];
}
使用正则表达式匹配:

var Response = "TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,",
    CacheControlRegEx = /Cache-Control:\s(.+?),/,
    CacheControl = false;

if (CacheControl = CacheControlRegEx.exec(Response)){
    CacheControl = CacheControl[1];
}

使用split函数可以很好地工作,代码如下

var response = "TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,";
var splitted = response.split(",");
var cache_splitted = splitted[1].split(":");
var cache_val = cache_splitted[1];

Fiddle:

使用split函数可以很好地工作,代码如下

var response = "TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,";
var splitted = response.split(",");
var cache_splitted = splitted[1].split(":");
var cache_val = cache_splitted[1];

Fiddle:

使用split函数可以很好地工作,代码如下

var response = "TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,";
var splitted = response.split(",");
var cache_splitted = splitted[1].split(":");
var cache_val = cache_splitted[1];

Fiddle:

使用split函数可以很好地工作,代码如下

var response = "TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,";
var splitted = response.split(",");
var cache_splitted = splitted[1].split(":");
var cache_val = cache_splitted[1];

Fiddle:

您需要使用多个拆分:

var string = "TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,";
var all = {};
/* First split the string at the comma */
string = string.split(",");
/* Now loop through the created array */
for(var o = 0; o < string.length; o++){
    /* Splitting each element again, this time at the `:` */
    var temp = string[o].split(":");
    /* And assigning the first in the temporary element as the key
     * And then either a value or an empty string. I used .trim() to 
     * make it easier, it could be even easier if you 
     * use .toLowerCase() so you can't make typos. */
    all[temp[0].trim()] = temp[1] || "";
}

var string=“TotalHTTP/1.1 200 OK,缓存控制:最大年龄=900,公共,连接:保持活动,日期:Fri,2014年12月19日06:54:04 GMT,内容长度:28359,内容类型:text/html;字符集=utf-8,a:b,”;
var all={};
/*首先在逗号处拆分字符串*/
string=string.split(“,”);
/*现在循环创建的数组*/
for(var o=0;o
您需要使用多个拆分:

var string = "TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,";
var all = {};
/* First split the string at the comma */
string = string.split(",");
/* Now loop through the created array */
for(var o = 0; o < string.length; o++){
    /* Splitting each element again, this time at the `:` */
    var temp = string[o].split(":");
    /* And assigning the first in the temporary element as the key
     * And then either a value or an empty string. I used .trim() to 
     * make it easier, it could be even easier if you 
     * use .toLowerCase() so you can't make typos. */
    all[temp[0].trim()] = temp[1] || "";
}

var string=“TotalHTTP/1.1 200 OK,缓存控制:最大年龄=900,公共,连接:保持活动,日期:Fri,2014年12月19日06:54:04 GMT,内容长度:28359,内容类型:text/html;字符集=utf-8,a:b,”;
var all={};
/*首先在逗号处拆分字符串*/
string=string.split(“,”);
/*现在循环创建的数组*/
for(var o=0;o
您需要使用多个拆分:

var string = "TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,";
var all = {};
/* First split the string at the comma */
string = string.split(",");
/* Now loop through the created array */
for(var o = 0; o < string.length; o++){
    /* Splitting each element again, this time at the `:` */
    var temp = string[o].split(":");
    /* And assigning the first in the temporary element as the key
     * And then either a value or an empty string. I used .trim() to 
     * make it easier, it could be even easier if you 
     * use .toLowerCase() so you can't make typos. */
    all[temp[0].trim()] = temp[1] || "";
}

var string=“TotalHTTP/1.1 200 OK,缓存控制:最大年龄=900,公共,连接:保持活动,日期:Fri,2014年12月19日06:54:04 GMT,内容长度:28359,内容类型:text/html;字符集=utf-8,a:b,”;
var all={};
/*首先在逗号处拆分字符串*/
string=string.split(“,”);
/*现在循环创建的数组*/
for(var o=0;o
您需要使用多个拆分:

var string = "TotalHTTP/1.1 200 OK,Cache-Control: max-age=900, public,Connection: keep-alive,Date: Fri, 19 Dec 2014 06:54:04 GMT,Content-Length: 28359,Content-Type: text/html; charset=utf-8,a:b,";
var all = {};
/* First split the string at the comma */
string = string.split(",");
/* Now loop through the created array */
for(var o = 0; o < string.length; o++){
    /* Splitting each element again, this time at the `:` */
    var temp = string[o].split(":");
    /* And assigning the first in the temporary element as the key
     * And then either a value or an empty string. I used .trim() to 
     * make it easier, it could be even easier if you 
     * use .toLowerCase() so you can't make typos. */
    all[temp[0].trim()] = temp[1] || "";
}

var string=“TotalHTTP/1.1 200 OK,缓存控制:最大年龄=900,公共,连接:保持活动,日期:Fri,2014年12月19日06:54:04 GMT,内容长度:28359,内容类型:text/html;字符集=utf-8,a:b,”;
var all={};
/*首先在逗号处拆分字符串*/
string=string.split(“,”);
/*现在循环创建的数组*/
for(var o=0;o
非常感谢您!!这是我的要求!!:)非常感谢你!!这是我的要求!!:)非常感谢你!!这是我的要求!!:)非常感谢你!!这是我的要求!!:)