简体   繁体   English

Javascript将字符串拆分成数组字典(键->值)(正则表达式)

[英]Javascript split a string into an array dictionary (key -> value) (regex)

The aim is to parse a string into an array dictionary in javascript. 目的是将字符串解析为javascript中的数组字典。

For example this maybe the string that needs to be parsed 例如,这可能是需要解析的字符串

"k=23:3/24:32b=43:532:45/3:3253" “K = 23:3/24:32B = 43:532:45/3:3253”


I would like that string to turn into a dictionary like so (Key - Value) 我希望该字符串变成这样的字典(键-值)

k - 23:3/24:32 k-23:3/24:32

b - 43:532:45/3:3253 b-43:532:45/3:3253


My initial idea was to search for [aZ]\\*.* and split it into matches using regex. 我最初的想法是搜索[aZ] \\ *。*并使用正则表达式将其拆分为匹配项。

However, I do not think this would work as this would also bring over b which is not what I want. 但是,我认为这不会奏效,因为这也会带来b ,这不是我想要的。 Also, I was not able to get this to work (I'm new to regex). 另外,我无法使它正常工作(我是regex的新手)。


An equals will only ever be between a key and variable (never in the value). 等于将永远只在键和变量之间(永远不在值中)。 The key will also only ever be a single character not a word. 密钥也只能是单个字符而不是单词。

var test = "k=23:3/24:32b=43:532:45/3:3253";
var r = /(.*)([a-Z])(//*)(*.)/

Above was my idea of going about it but I can not seem to get anything to work. 上面是我打算做的事情,但是我似乎什么也没做。

Possibly use /.=[^=]+(?!=)/g to match the key value pair without further knowledge of what characters are possible for the key and value: 可能使用/.=[^=]+(?!=)/g来匹配键值对,而无需进一步了解键和值可以使用哪些字符:

  • Here .= matches the key (single character) right before the equal sign; .=与等号前的键(单个字符)匹配;
  • [^=]+(?!=) matches all non = characters after until one character before the next equal sign (restrict the greedy match using a negative look ahead) or the end of string; [^=]+(?!=)匹配所有非=字符,直到下一个等号之前的一个字符(使用负向前看限制贪婪匹配)或字符串的末尾;

 var test = "k=23:3/24:32b=43:532:45/3:3253"; var result = {} test.match(/.=[^=]+(?!=)/g).forEach( m => { var [k, v] = m.split('=') result[k] = v } ) console.log(result) 

Since the identifier is only one character and the = is where the identifier ends and the value begins, then just do that using a regex like: 由于标识符只有一个字符,而=则是标识符结束且值开始的位置,因此只需使用正则表达式即可,例如:

var r = /.=.+?(?=(.=)|$)/g;

Which means: 意思是:

.          : a character
=          : followed by =
.+?        : followed by a bunch of characters (the ? means non-greedy mode: match as few as possible so that it won't consume from other variables)
(?=(.=)|$) : positive look-ahead, means that the above should be followed either by another .= (a character that is followed by =) or the end of the string

Then you can reduce the matches to get your desired object, by split ing each match to get the key-value pairs: 然后,通过split每个匹配项以获取键值对,可以reduce匹配项以获取所需的对象:

var obj = test.match(r).reduce(function(obj, match) {
    var keyValue = match.split("=");
    obj[keyValue[0]] = keyValue[1];
    return obj;
}, {});

Example: 例:

 var test = "k=23:3/24:32b=43:532:45/3:3253"; var r = /.=.+?(?=(.=)|$)/g; var obj = test.match(r).reduce(function(obj, match) { var keyValue = match.split("="); obj[keyValue[0]] = keyValue[1]; return obj; }, {}); console.log(obj); 

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

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