I've got a string which looks something like this:
PRE_VALUE1="TIMESTAMP" PRE_VALUE2="String13" PRE_VALUE3="" PRE_VALUE4="1" PRE_VALUE5="AK" PRE_VALUE6="key6" POST_VALUE1="000" VALUE8="KE_Y1" PRE_VALUE9="" PRE_VALUE10="10.10.10.10" ....
My goal is to split the string after the second quote mark for each PRE_VALUE#
. After that I want to build a simple RegExp which checks if the PRE_VALUE# has something in it's quote marks.
The part above is almost clear to me. I'll use the split-Method - Here is my issue that I'm not sure how to tell the split-Method that it should split the string at the second quote mark and not the first.
However, my real concern is that I want to assign the splitted values if they match a RexExp to a variable. Before they got assigned to that variable there should be a simple word which explains the field. e.g.:
PRE_VALUE2="String13" is going to be "String: String13".
If I understood it correctly the best and fastest way is to use a multidimensional array here. As there a many of those messages and "PRE_VALUE#"'s building it only via RegExp for the full input string should be kinda slow, correct?
Maybe someone can help me out here.
Thanks in advance!
-
splitting on each other quote isn't possible as such with the split function, but you can certainly split on quotation marks and consider even indexes "n" in the resulting array the variable name, and odd indexes "n+1" the variable value. There's certainly a better way to do that with regexes, but I'm still not too good with these...Laurent S.– Laurent S.2020年02月07日 07:43:54 +00:00Commented Feb 7, 2020 at 7:43
2 Answers 2
Instead of using just a Regex to filter out the PRE_VALUEx
values, you can split the string
by spaces then by =
, to get all the relevant values
in an array
like this:
[ [ "PRE_VALUE1", "TIMESTAMP" ], [ "PRE_VALUE2", "String13" ], [ "PRE_VALUE3", "" ], [ "PRE_VALUE4", "1" ], [ "PRE_VALUE5", "AK" ], [ "PRE_VALUE6", "key6" ], [ "POST_VALUE1", "000" ], [ "VALUE8", "KE_Y1" ], [ "PRE_VALUE9", "" ], [ "PRE_VALUE10", "10.10.10.10" ] ]
This is how should be the code:
var arr = string.split("\" ").map(x => {
x = x.replace(/\"/g, '');
return x.split("=");
});
Demo:
let string = `PRE_VALUE1="TIMESTAMP" PRE_VALUE2="String13" PRE_VALUE3="" PRE_VALUE4="1" PRE_VALUE5="AK" PRE_VALUE6="key6" POST_VALUE1="000" VALUE8="KE_Y1" PRE_VALUE9="" PRE_VALUE10="10.10.10.10"`;
var arr = string.split("\" ").map(x => {
x = x.replace(/\"/g, '');
return x.split("=");
});
console.log(arr);
-
what if the space is inside of quotes?Nina Scholz– Nina Scholz2020年02月07日 08:14:21 +00:00Commented Feb 7, 2020 at 8:14
-
In that case we will split by
"
to make sure to split by the relevant spaces.cнŝdk– cнŝdk2020年02月07日 08:17:29 +00:00Commented Feb 7, 2020 at 8:17 -
Is there a way to change the prefix e.g. "PRE_VALUE1" in the strings when returning them? And also to save every return value in a separate variable so that i can e.g. log "var1 has the value TIMESTAMP" and "var2 has the value of String13"?qwertzy– qwertzy2020年02月07日 08:21:24 +00:00Commented Feb 7, 2020 at 8:21
-
@qwertzy In that case instead of returning
return x.split("=")
usereturn x.split("=")[1]
to get only the values inside your array, then you can iterate over the array and print the variables.cнŝdk– cнŝdk2020年02月07日 08:29:49 +00:00Commented Feb 7, 2020 at 8:29
You could search for not quoting characters inside of the quote.
var string = 'PRE_VALUE1="TIMESTAMP" PRE_VALUE2="String13" PRE_VALUE3="" PRE_VALUE4="1" PRE_VALUE5="AK" PRE_VALUE6="key6" POST_VALUE1="000" VALUE8="KE_Y1" PRE_VALUE9="" PRE_VALUE10="10.10.10.10"',
pairs = string
.match(/[^\s=]+="[^"]*"/g)
.reduce((r, s) => {
var [, k, v] = s.match(/([^\s=]+)="([^"]*)"/);
r[k] = v;
return r;
}, {});
console.log(pairs);
.as-console-wrapper { max-height: 100% !important; top: 0; }
A more direct approach
var regex = /([^\s=]+)="([^"]*)"/gm,
string = 'PRE_VALUE1="TIMESTAMP" PRE_VALUE2="String13" PRE_VALUE3="" PRE_VALUE4="1" PRE_VALUE5="AK" PRE_VALUE6="key6" POST_VALUE1="000" VALUE8="KE_Y1" PRE_VALUE9="" PRE_VALUE10="10.10.10.10"',
pairs = {},
m;
while ((m = regex.exec(string)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) regex.lastIndex++;
let [, k, v] = m;
pairs[k] = v;
}
console.log(pairs);
.as-console-wrapper { max-height: 100% !important; top: 0; }
-
Hi, thanks for the quick response and your help. The solutions looks kinda perfect to me - Is there a way to change the prefix e.g. "PRE_VALUE1" in the strings when returning them? And also to save every return value in a separate variable?qwertzy– qwertzy2020年02月07日 07:59:49 +00:00Commented Feb 7, 2020 at 7:59
-
@qwertzy Welcome to SO! What should I do when someone answers my question? :)StepUp– StepUp2020年02月07日 08:13:12 +00:00Commented Feb 7, 2020 at 8:13