简体   繁体   English

javascript从正则表达式匹配字符串中获取字符串

[英]javascript get string out of parantheses match regex

I have a full string consisting of something like this [(data1.1)(data1.2)][(data2.1)(data2.1)] 我有一个完整的字符串,其中包括这样的内容:[(data1.1)(data1.2)] [(data2.1)(data2.1)]

Ive read you can do something smart with match function and regex. 我读过,您可以使用match函数和regex做一些聪明的事情。 I want two arrays with the data loaded out of the parentheses.. How in earth do i do that? 我希望两个数组的数据从括号中装入。.在地球上我该怎么做? Please use my string as example. 请使用我的字符串作为示例。

I came up with this abomination: 我想到了这个可憎的事情:

var array = s.replace(/^\[|\]$/g,'').split('][').map(function(a){    
    return a.replace(/^\(|\)$/g,'').split(')(')
});

http://jsfiddle.net/8kLhc/ http://jsfiddle.net/8kLhc/

Also if you deliberately save or transmit data like this then you should really have a look at JSON or proper database design. 另外,如果您故意像这样保存或传输数据,那么您应该真正了解JSON或适当的数据库设计。

I wasn't entirely sure what you were after, but I'm assuming this 我不太确定你在追求什么,但我假设这是

var what = '[(data1.1)(data1.2)][(data2.1)(data2.2)]',
    have = [],
    you = [],
    tried = /\[\(([^)]*)\)\(([^)]*)\)\]/g;
what.replace(tried, function (use, brain, forthis) {
    you.push((have.push(brain), forthis));
});
console.log(have, you);
// ["data1.1", "data2.1"] ["data1.2", "data2.2"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM