Created
October 23, 2018 13:51
-
-
Save kyya/92dd3150bcfafdf8f20546de0ce05ee2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 源字符串 | |
| const str1 = " abc =b\\n ;c=\\x61d;d=234;t=\\n;d=\"test;yes \";" | |
| // 考虑 Value 内包含 `\"` 或 `=` 的 | |
| const str2 = " abc =b\\n ;c=\\x61d;d=234;t=\\n;d=\"test;yes\";g=\"\";sdsd\"" | |
| // 考虑 Value 内包含空格的 | |
| const str3 = " abc =b\\n ;c=\\x61d;d=234;t=\\n;d=\"test;yes\";g=\"\" ; s dsd\"" | |
| const decode = function (str) { | |
| var res = [] | |
| str.match(/(\w+[\s]*=((\".*?\")|(.*?));)/g).map(_item=>{ | |
| let item = _item.replace(/\s+/g, '').replace(/;$/, '') | |
| // console.log(item) | |
| let K = item.split('=')[0] | |
| let V = item.split('=')[1] | |
| res[K] = V | |
| }) | |
| return res | |
| } | |
| console.log(decode(str1)) // [ abc: 'b\\n', c: '\\x61d', d: '"test;yes"', t: '\\n' ] | |
| // console.log(decode(str2)) | |
| // console.log(decode(str3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment