Skip to content

Instantly share code, notes, and snippets.

@JacksonTian
Last active May 12, 2016 16:50
Show Gist options
  • Save JacksonTian/1f2838b5402db1c6531e63b5b4f6e252 to your computer and use it in GitHub Desktop.
Save JacksonTian/1f2838b5402db1c6531e63b5b4f6e252 to your computer and use it in GitHub Desktop.
正则表达式练手

问题描述:

将一个字符串:

"key1='value11,value12,value13',key2=value2,key3='value3',key4=value4"

提取成:

[
  "key1='value11,value12,value13'",
  "key2=value2",
  "key3='value3'",
  "key4=value4"
]

的形式。

var str = "key1='value11,value12,value13',key2=value2,key3='value3',key4=value4";
var patt = /[^=,]+=('[^']*'|[^,]*)/g
var match;
var list = [];
while ((match = patt.exec(str))) {
list.push(match[0]);
}
console.log(list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment