Get a requirement to make a rule in string form editable.
Also is to
A≥1 or A≥1&B < 3
Turn left,
Components: A B C D T
Symbol: > < ≥ ≤ =
Connector: & |
Ideas:
The rules of the string form are cut first, and then the cut string fragments are stored in the corresponding variables because they need to be verified.
Specific ideas:
- According to the concatenation, it is first cut into multiple rule fragments, and each rule fragment corresponds to an array index
- Then decompose and cut according to each regular fragment
- The class is merged into the corresponding attribute
Code practices:
// Cut the string rules returned in the background into form rendering
parseDomins(data) {
let domains = [];
data = data.replace(/&/g."@ &");
data = data.replace(/[|]/g."@ |");
data.split("@").forEach(item= > {
if (item == "") return;
domains.push({});
item = item.replace(/&/g.& "@");
item = item.replace(/[|]/g.| "@");
item = item.replace(/ p/g.@ @ "or");
item = item.replace(/ / g or less."@ @ or less");
item = item.replace(/>/g."@ > @");
item = item.replace(/=/g.@ = "@");
item = item.replace(/</g."@" @");
item.split("@").forEach(subitem= > {
if (item == "") return;
switch (subitem) {
case "&":
case "|":
if (domains[domains.length - 1].connectionSymbol)
domains[domains.length - 1].connectionSymbol += subitem;
else domains[domains.length - 1].connectionSymbol = subitem;
break;
case "A":
case "B":
case "C":
case "D":
case "T":
if (domains[domains.length - 1].elementType)
domains[domains.length - 1].elementType += subitem;
else domains[domains.length - 1].elementType = subitem;
break;
case "≥":
case "Or less":
case ">":
case "<":
case "=":
if (domains[domains.length - 1].symbol)
domains[domains.length - 1].symbol += subitem;
else domains[domains.length - 1].symbol = subitem;
break;
default:
if (domains[domains.length - 1].num)
domains[domains.length - 1].num += subitem;
else domains[domains.length - 1].num = subitem;
break; }}); });return domains;
},
Copy the code
Results:
Optimized execution code
This is optimized according to the method given by Lao Yao’s comments, you can go to the comment section to see ~
// Cut the string rules returned in the background into form rendering (used with user tag modification)
parseDomins(data) {
let temp = [];
data = data.replace(/(\&|\|)/g, (match, p1) => ` @${p1}`);
data.split("@").forEach(item= > {
temp.push({});
item.replace(
/ (\ & | \ |)? ([ABCDT]) (> | < | | | = or less or higher)/(\ d +),
(match, connectionSymbol, elementType, symbol, num) => {
temp[temp.length - 1].connectionSymbol = connectionSymbol;
temp[temp.length - 1].elementType = elementType;
temp[temp.length - 1].symbol = symbol;
temp[temp.length - 1].num = num; }); });return temp;
},
Copy the code