gpt4 book ai didi

javascript - 拆分 Javascript 字符串

转载 作者:行者123 更新时间:2023-11-29 10:49:44 24 4
gpt4 key购买 nike

我有一个名为 cookie 的 javascript 字符串,它看起来像这样:

__utma=43024181.320516738.1346827407.1349695412.1349761990.10; __utmz=43024181.1346827407.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=43024181.19.10.1349761990; __utmc=43024181; language=en

它可以有更多;xxxxxx;但条目总是被;包围。现在我想将我的 var 拆分成一个数组并搜索条目“language=xy”,该条目应保存在“newCookie”中。

谁能帮帮我,我完全无法将 var 拆分为数组并搜索条目。

感谢帮助和分享

最佳答案

var cookie = '__utma=43024181.320516738.1346827407.1349695412.1349761990.10; __utmz=43024181.1346827407.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=43024181.19.10.1349761990; __utmc=43024181; language=en;';

var cookie_array = cookie.split(';'); // Create an Array of all cookie values.

// cookie_array[0] = '__utma=43024181.320516738.1346827407.1349695412.1349761990.10'
// cookie_array[1] = '__utmz=43024181.1346827407.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)'
// cookie_array[2] = '__utmb=43024181.19.10.1349761990'
// cookie_array[3] = '__utmc=43024181'
// cookie_array[4] = 'language=en'

var size = cookie_array.length; // Get Array size to prevent doing lookups in a loop.

for (var i = 0; i < size; i++) {
var keyval = cookie_array[i].split('='); // Split into a key value array

// What we're trying to find now.
// keyval[0] = 'language'
// keyval[1] = 'en'

if (keyval[0] == 'language') { //keyval[0] is left of the '='
//write new cookie value here
console.log('Language is set to ' + keyval[1]); // keyval[1] is right side of '='
}
}

希望这对你有帮助。

有关 split() 方法的更多信息,请参阅 split() Mozilla Developer Network (MDN) documentation

关于javascript - 拆分 Javascript 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12793814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com