正则表达式 - 从结果中删除方括号

时间:2015-04-20 11:18:54

标签: javascript regex

我有一个字符串如下:

var str = 'Array
(
    [shopUsername] => tod
    [password] => 12345678
    [shopID] => 19
)';

我尝试使用以下正则表达式在JavaScript中解析它:

var matches = parsedXMLStr.match(/\[(.*)\]/g);

将每个键值返回为[ '[shopUserName]', '[password]', '[shopID]' ]

如何更改正则表达式以从返回的值中去掉方括号,即数组应为['shopUserName', 'password','shopID']

2 个答案:

答案 0 :(得分:3)

使用exec抓取群组:

var str = 'Array(    [shopUsername] => tod\n    [password] => 12345678\n    [shopID] => 19)';
var match;
var results = [];
var regex = new RegExp(/\[(.*)\]/g);
while(match = regex.exec(str)) {
    results.push(match[1]);
}
console.log(results);

jsFiddle

答案 1 :(得分:0)

尝试使用

str.replace(/[\[\]']+/g,'')
相关问题