2

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!

asked Feb 7, 2020 at 7:34
1
  • 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... Commented Feb 7, 2020 at 7:43

2 Answers 2

1

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);

answered Feb 7, 2020 at 8:11
4
  • what if the space is inside of quotes? Commented Feb 7, 2020 at 8:14
  • In that case we will split by " to make sure to split by the relevant spaces. Commented 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"? Commented Feb 7, 2020 at 8:21
  • @qwertzy In that case instead of returning return x.split("=") use return x.split("=")[1] to get only the values inside your array, then you can iterate over the array and print the variables. Commented Feb 7, 2020 at 8:29
1

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; }

answered Feb 7, 2020 at 7:43
2
  • 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? Commented Feb 7, 2020 at 7:59
  • @qwertzy Welcome to SO! What should I do when someone answers my question? :) Commented Feb 7, 2020 at 8:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.