I want to extract values/variables from boolean query For e.g
The boolean query is:
("Asset Data Management") AND "Data Scientist" AND ("Data Science & Analysis" OR "Financial Modelling" OR "Credit Structuring") AND ("Goldman Sachs" OR "Deutsche Bank" OR "BNY Mellon" OR "JP Morgan" OR BlackRock OR Amazon)
The expected output must be:
["Asset Data Management","Data Scientist","Data Science & Analysis","Financial Modelling","Credit Structuring","Goldman Sachs","BNY Mellon","JP Morgan","BlackRock","Amazon"]
You can simply achieve this by using regex to perform string split() and replaceAll() operation.
/AND|OR/g - This regex is used to split the original string with AND and OR and will return the array.
/"|\(|\)/ig - This regex is used to replace all the (, ) & " with the empty string.
Working Demo :
const str = '("Asset Data Management") AND "Data Scientist" AND ("Data Science & Analysis" OR "Financial Modelling" OR "Credit Structuring") AND ("Goldman Sachs" OR "Deutsche Bank" OR "BNY Mellon" OR "JP Morgan" OR BlackRock OR Amazon)';
const strArr = str.split(/AND|OR/g);
const finalArr = strArr.map(elem => {
return elem.trim().replaceAll(/"|\(|\)/g, '')
});
console.log(finalArr);