Get Substring between two characters in javascript

2019-09-02 18:21发布

After searching SO and other articles (all in vein) a few days before I asked a question about getting numbers between two characters using Javascript only. But unforunately I wanted to grab substring not just numbers from a string using javascript only. I have this string

var str = 'a:7:{i:0;s:1:"1";i:1;s:12:"John Smith";i:2;s:19:"My Life Begins Here";i:3;s:31:"This is my .Picture.jpg";i:4;s:10:"1988-07-26";}'

and solution to grab only numbers from string this works great

str.match(/"\d+"/g).join().replace(/"/g,'');

but this doesn't work if we need to grab substring, I tried removing \d from regex but that didn't work.

What is wrong I am doing? The output of this can be something like this

 //An array like this
 array = ['1','John Smith', 'My Life Begins Here', 'This is my .Picture.jpg', '1988-07-26'];

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.

1条回答
做自己的国王
2楼-- · 2019-09-02 19:02

Like this? It will still break with escaped \" characters.

str.match(/".*?"/g).map(function(str) {
  return str.substring(1, str.length - 1);
});
查看更多
登录 后发表回答