I need to listen to an input and get its value dynamically,when a specific "flag" happens to be typed,get whatever is typed next until a flag appears again.
let me explain :
lets say i have an array of "flags"
let flags = ['foo','bar','baz']
and i have an input to listen which gives me the following string (dynamically,character by character):
let input = "whateveridontneedthatfoodavidbarjennifer-andrew-billbazericfoojohnbarchristen"
*foo
and bar
appear twice and baz
once
i want somehow to create,maybe an object like this :
{
foo: ["david","john"],
bar: ["jennifer-andrew-bill","christen"],
baz: ["eric"]
}
or 3 separate arrays,i dont really care about the structure as long i filter the value properly
CodePudding user response:
First, construct a regex:
let regex = '('; // parenthesis is added for capturing groups, which means the flags will be captured too
for (f of flags) regex = f '|';
regex = new RegExp(regex.substring(0, regex.length - 1) ')', 'g'); // construct the regex with global flag
// regex = /(foo|bar|baz)/g
Then, split the string:
s = s.split(regex) // ["whateveridontneedthat", "foo", "david", "bar", "jennifer-andrew-bill", "baz", "eric", "foo", "john", "bar", "christen"]
Finally, loop through the splitted string and add them to the object.
Below is the full example:
let flags = ['foo', 'bar', 'baz'];
let s = "whateveridontneedthatfoodavidbarjennifer-andrew-billbazericfoojohnbarchristen";
let regex = '(';
let obj = {};
for (f of flags) regex = f '|';
regex = new RegExp(regex.substring(0, regex.length - 1) ')', 'g');
s = s.split(regex);
for (let i = 0; i < s.length; i ) {
for (let j = 0; j < flags.length; j ) { // checking for flags
if (s[i] == flags[j]) { // found the flag
if (flags[j] in obj) obj[flags[j]].push(s[i 1]); // array exist in object
else obj[flags[j]] = [s[i 1]]; // create array in object
i ; // skip next s[i]
break; // s[i] can only be one of the flag
}
}
}
console.log(obj);
CodePudding user response:
Good answer from @Apple BS, I just want to propose this syntax:
const flags = ['foo', 'bar', 'baz']
const str = 'whateveridontneedthatfoodavidbarjennifer-andrew-billbazericfoojohnbarchristen'
const strSplit = str.split(new RegExp(`(${flags.join('|')})`, 'g'))
const obj = Object.fromEntries(flags.map(f => [f, []]))
strSplit.forEach((el, i) => {
if (flags.includes(el)) obj[el].push(strSplit[i 1])
})
console.log(obj)