0

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.

  • http://stackoverflow.com/questions/14867835/get-substring-between-two-characters-using-javascript – Rob Sedgwick Feb 28 '14 at 23:12
  • No @RobSedgwick that's different I tried that –  Feb 28 '14 at 23:12
  • What substring are you looking for? – Qantas 94 Heavy Feb 28 '14 at 23:14
  • Oh, Okay. Well always worth putting that in your question. The first thing we do is look to see if it's a dupe. Good luck. – Rob Sedgwick Feb 28 '14 at 23:14
  • It seems to me you are generating a string with PHP `serialize()` and passing it to JavaScript. Why don't you use `json_encode()` instead and pass to JavaScript a struct that is in fact written in JavaScript synthax? – Havenard Feb 28 '14 at 23:15
  • @Havenard please see I have updated my question for what you said –  Feb 28 '14 at 23:17
  • I think this is what you really need. https://www.npmjs.org/package/php-unserialize "JavaScript tool to unserialize data taken from PHP. It can parse "serialize()" output, or even serialized sessions data." – Havenard Feb 28 '14 at 23:18
  • I wanted to do it with regex not using any external library –  Feb 28 '14 at 23:19
  • 1
    RegEx don't have what it takes to parse this information properly for the same reason [you cannot parse HTML with RegEx](http://stackoverflow.com/a/1732454/156811) – Havenard Feb 28 '14 at 23:20
  • but @Havenard what if can parse this like the has suggested regex. This generates exactly the same? –  Feb 28 '14 at 23:23
  • 2
    The serialized data can countain multiple layers (arrays inside arrays) and RegEx is simply incapable of handling that. It also can have characters inside strings that may be interpreted by RegEx as the string delimiter while that might be not the case. Also, RegEx face everything as a string, even the numbers, while that might be not the case either. It is specially important in JavaScript because while 1+1 is 2, "1"+1 is 11. – Havenard Feb 28 '14 at 23:27
  • Oh great notice. I just ignored that @Havenard. Thanks man this really something important to consider Specially characters inside Strings. –  Feb 28 '14 at 23:35
  • @Havenard I am really happy that you pointed that –  Feb 28 '14 at 23:50
  • And now I definetly would go with npm php-unserialize module –  Feb 28 '14 at 23:51

1 Answers1

2

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

str.match(/".*?"/g).map(function(str) {
  return str.substring(1, str.length - 1);
});
sabof
  • 8,062
  • 4
  • 28
  • 52
  • Yes but substring should be in ' ' and one thing that your regex removes . from substring like This is my .Picture.jpg is becoming like This is my Picture –  Feb 28 '14 at 23:14
  • No, it doesn't remove dots – sabof Feb 28 '14 at 23:16
  • thanks for your help but your regex is giving this '1','John Smith','My Life Begins Here','This is my Picture','1988-07-26' –  Feb 28 '14 at 23:18
  • Are you sure you are testing on the same string as in the example? – sabof Feb 28 '14 at 23:20
  • Oh My GOD! My mistake. That works great thanks Sabof Thanks alot. –  Feb 28 '14 at 23:21