Challenge of the day!
The code in question simply scans a string for ${} patterns and replaces them with text corresponding to the pattern in a JSON file.
Code:getText = function(text) {
let regex = /\${([\w|\.]+)}/gm;
let regexParts;
console.log("Start process text:", text);
do {
regexParts = regex.exec(text);
if (regexParts) {
console.log("Find pattern:", regexParts[0]);
text = text.replace(regexParts[0], ODW.MLS.getTextDatabase(regexParts[1]));
console.log("New decoded text:", text);
}
} while (regexParts);
console.log("Get result:", text);
return text;
};
getText("${Name} ${Element}");
getText("${FName} ${LName}");
I use a single JSON file so the value-keys are retrieved by my
Multi-Language System plugin via the ODW.MLS.getTextDatabase() function. The content of the JSON file is as follows:
Code:{
"FName": "Reid",
"LName": "Bruteforce",
"Name": "Batman",
"Element": "Fire"
}
And here is the result of the console when I run the script.
So can someone explain to me why the first string is decoded completely, while in the second, only the first pattern is decoded?
:kaodes:
Thanks in advance for your help!
Edit: Additional tests for fun, this time without using the function of my plugin. I don't understand what I'm missing...
Simply by removing the call to the function.
Code:getText = function(text) {
let regex = /\${([\w|\.]+)}/gm;
let regexParts;
console.log("Start process text:", text);
do {
regexParts = regex.exec(text);
if (regexParts) {
console.log("Find pattern:", regexParts[0]);
text = text.replace(regexParts[0], regexParts[1]);
console.log("New decoded text:", text);
}
} while (regexParts);
console.log("Get result:", text);
return text;
};
getText("${Name} ${Element}");
getText("${FName} ${LName}");
Result: only the first pattern is detected in both strings.

By replacing the function call, and concatenating a text to the found pattern.
Code:getText = function(text) {
let regex = /\${([\w|\.]+)}/gm;
let regexParts;
console.log("Start process text:", text);
do {
regexParts = regex.exec(text);
if (regexParts) {
console.log("Find pattern:", regexParts[0]);
text = text.replace(regexParts[0], regexParts[1] + "OK");
console.log("New decoded text:", text);
}
} while (regexParts);
console.log("Get result:", text);
return text;
};
getText("${Name} ${Element}");
getText("${FName} ${LName}");
Result: the two patterns of each string are decoded.
