在JavaScript中的两个字符之间获取子字符串 [英] Get Substring between two characters in javascript

查看:126
本文介绍了在JavaScript中的两个字符之间获取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前搜索了SO和其他文章(全部内容)后,我问了一个有关仅使用Javascript在两个字符之间获取数字的问题.但不幸的是,我只想从JavaScript中获取 substring ,而不仅仅是字符串中的数字.我有这个字符串

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,'');

但是如果我们需要获取子字符串,这是行不通的,我尝试从正则表达式中删除\ d,但是那行不通.

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'];

实际上,上面的字符串是存储在MemcacheD中的PHP数组,我正在Node.JS中检索它,这就是为什么我不能使用json_encode的原因.我不擅长Regex.因此,请专家对此有所启发.

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.

推荐答案

喜欢吗?它仍然会以转义的\字符断开.

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

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

这篇关于在JavaScript中的两个字符之间获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆