I am making an application where I have a string from which I will form an array with the help of regex.
Eg..,
String: DEVICE_SIZE IN ('036','048', '060','070')
Output:
{
"DEVICE_SIZE": [
"036",
"048",
"060",
"070"
]
}
Like this for all string, we follow the following code to get the array data.
Here if there is a NOT keyword before the key, then we add NOT *key* like "NOT DEVICE_DISCHARGE_AIR"
Requirement:
I have a string like NOT (DEVICE_SERIES IN ('LV') AND DEVICE_VOLTAGE IN ('A','8') AND DEVICE_SIZE IN ('007','009','012','018')).
So here after NOT there is a parenthesis, so after NOT if there is a parenthesis then I need to add the key as
"NOT DEVICE_SERIES":["LV"],
"NOT DEVICE_VOLTAGE":["A", "8"] ,
"NOT DEVICE_SIZE": ['007','009','012','018']
Current scenario:
But right now, it is not adding the NOT keyword before of key.
Working Example:
const stringOne = "DEVICE_SIZE IN ('036','048', '060','070') AND DEVICE_VOLTAGE IN ('1','3') AND NOT DEVICE_DISCHARGE_AIR IN ('S') AND NOT DEVICE_REFRIGERANT_CIRCUIT IN ('H', 'C')";
const stringTwo = "DEVICE_SERIES IN ('LV') AND DEVICE_ELECTRICAL IN ('K') AND NOT (DEVICE_SERIES IN ('LV') AND DEVICE_VOLTAGE IN ('A','8') AND DEVICE_SIZE IN ('007','009','012','018'))";
const regex = /((?:\bNOT\s+)?\w+)\s+IN\s+\('([^()]*)'\)/g;
const getTransformedData = (string) => {
return Array.from(
string.matchAll(regex), m =>
({
[m[1]]: m[2].split(/',\s*'/)
})
)
}
console.log(getTransformedData(stringOne)); // Working fine
console.log(getTransformedData(stringTwo)); // Need to include NOT infront of each key as it is union
How can I add NOT keyword in front of each key if it comes with the pattern of NOT and then the parenthesis like in stringTwo?
Expected result:
"NOT DEVICE_SERIES":["LV"],
"NOT DEVICE_VOLTAGE":["A", "8"] ,
"NOT DEVICE_SIZE": ['007','009','012','018']