获取javascript中两个字符之间的子字符串

时间:2023-01-04 17:06:50

First of all, I tried to search it on SO but couldn't find a solution. I have a string

首先,我试着在上面搜索,但是没有找到解决方案。我有一个字符串

var str = 'a:5:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";i:4;s:1:"5";}';

I want to grab all numbers between " " as an array in javascript. I tried this suggested on one of the SO questions.

我想以javascript中的数组形式获取" "之间的所有数字。我试过其中一个问题。

console.log(str.split(/[""]/));

but this outputs

但是这个输出

["a:5{i:0;s:1:", "1", ";i:1;:", "2", ";i:2;s:1:", "7", ";i:3;s:1:", "4", ";i:4;s:1:", "5", ";}"]

which is not exact.

这不是准确的。

Actually that above string is an array of PHP stored in MemcacheD and I am retrieving it in Node.JS and that's why I can't use json_encode or so. I am not good at Regex. So Experts please show some shine on it.

实际上,上面的字符串是存储在MemcacheD中的PHP数组,我正在节点中检索它。这就是为什么我不能使用json_encode或其他。我不擅长Regex。所以专家们请给我们一些启示。

The result of above string should be an array or string like this

上面字符串的结果应该是这样的数组或字符串

str = "1,2,3,4,5"; 

or an array like

或数组

array = [1,2,3,4,5];

3 个解决方案

#1


0  

So, this is a PHP serialized string. There is a version of unserialize available for javascript in the php.js project.

这是一个PHP序列化字符串。php中有一个版本的javascript可以使用unserialize。js的项目。

So using that function.

所以使用该函数。

Javascript

Javascript

var str = 'a:5:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";i:4;s:1:"5";}';

console.log(unserialize(str));

Output

输出

["1", "2", "3", "4", "5"] 

On jsFiddle

在jsFiddle

And if you are using nodejs then you could also consider a module like php-unserialize

如果您正在使用nodejs,那么您也可以考虑使用php-unserialize之类的模块

#2


3  

str.match(/"\d+"/g).join().replace(/"/g,'')
// "1,2,3,4,5"

#3


0  

function match_all(str, re) {
    var ret = [], v;
    while(v = re.exec(str)) {
        ret.push(v[1]);
    }
    return ret;
}
var str = 'a:5:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";i:4;s:1:"5";}';
var numbers = match_all(str, /"(\d+)"/g);
console.log(numbers);
//to convert the result to ints
console.log(numbers.map(function(v) { return +v; }));

#1


0  

So, this is a PHP serialized string. There is a version of unserialize available for javascript in the php.js project.

这是一个PHP序列化字符串。php中有一个版本的javascript可以使用unserialize。js的项目。

So using that function.

所以使用该函数。

Javascript

Javascript

var str = 'a:5:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";i:4;s:1:"5";}';

console.log(unserialize(str));

Output

输出

["1", "2", "3", "4", "5"] 

On jsFiddle

在jsFiddle

And if you are using nodejs then you could also consider a module like php-unserialize

如果您正在使用nodejs,那么您也可以考虑使用php-unserialize之类的模块

#2


3  

str.match(/"\d+"/g).join().replace(/"/g,'')
// "1,2,3,4,5"

#3


0  

function match_all(str, re) {
    var ret = [], v;
    while(v = re.exec(str)) {
        ret.push(v[1]);
    }
    return ret;
}
var str = 'a:5:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";i:4;s:1:"5";}';
var numbers = match_all(str, /"(\d+)"/g);
console.log(numbers);
//to convert the result to ints
console.log(numbers.map(function(v) { return +v; }));