Get Substring between two characters in javascript

我只是一个虾纸丫 提交于 2019-12-25 15:29:39

问题


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:


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

str.match(/".*?"/g).map(function(str) {
  return str.substring(1, str.length - 1);
});


来源:https://stackoverflow.com/questions/22107547/get-substring-between-two-characters-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!