Hi @chrscheuer,
Here is that question I had about replacing strings.
I've created a function to replace multiple strings from an array of strings. The problem is that I can't seem to inject a "" character into the replaceReg
ReGex to escape the parenthesis.
I'd like the replaceReg.toString()
to log this;
"\(remove this also\)$|\(remove this\)$|\(remove that\)$|\(but not this one\)$"
If I take away all the brackets/parenthesis from both arrays it works as expected.
Here is what I have so far;
let myArray = [
"Item 1 (remove this)",
"Item 2 (remove that)",
"Item 3 (remove this also)",
"Item but not this one 4",
];
let replaceStrings = [
"(remove this)",
"(remove that)",
"(remove this also)",
"but not this one",
];
/**
* @param {string[]} strings
* @param {string[]} targetStrings
*/
function removeSpecifiedStrings(arrayStrings, targetStrings) {
const replaceReg = RegExp(` ${targetStrings.join("$|")}$`, "g");
//Log the rereplaceReg string
log(replaceReg.toString());
return arrayStrings.map((string, i) => string.replace(replaceReg, ""));
}
let myResult = removeSpecifiedStrings(myArray, replaceStrings);
log(myResult);
The results are as follows;
log(replaceReg.toString()); => / (remove this)$|(remove that)$|(remove this also)$|but not this one$/g
log(myResult); => [
"Item 1 (remove this)",
"Item 2 (remove that)",
"Item 3 (remove this also)",
"Item but not this one 4"
]
My desired result would be;
log(myResult); => [
"Item 1",
"Item 2",
"Item 3",
"Item but not this one 4"
]
I could use something similar to the following method;
const myResult = myArray.map(x => x
.replace("(remove this)", '')
.replace("(remove that)", '')
.replace("(remove this also)", '')
.replace("but not this one", '')
);
log(myResult);
...but is there a way that I could use my first method to make it work, or better still another way?
I've also tried using String.raw
with no success.
I'm probably overthinking it. But if you have a suggestion that would be great.
Thanks in advance!
- Raphael Sepulveda @raphaelsepulveda2021-01-19 01:44:06.680Z
Kitch!
I'm sure Christian will provide an excellent answer for you but here's my take on it!
/** * @param {string[]} arrayStrings * @param {string[]} targetStrings */ function removeSpecifiedStrings(arrayStrings, targetStrings) { return arrayStrings.map(item => targetStrings.map(target => { if (item.includes(target)) return item.replace(target, ''); }).filter(i => i)[0] ); };
It's a spin-off of your second option, but just relying on the arrays themselves.
Interested to see a solution with RegEx thought, that's a doozy.
Kitch Membery @Kitch2021-01-19 01:47:18.594Z
Thanks, Mate!
Christian has answered this for me offline. I'll report back with the answer, however. He started by saying using RegEx for this is not a good idea.
Raphael Sepulveda @raphaelsepulveda2021-01-19 01:52:22.845Z
Ahh, I see. Yeah, that RegEx made me dizzy lol
- In reply toKitch⬆:Kitch Membery @Kitch2021-01-19 02:08:24.872Z
Here is the answer... as always thanks @chrscheuer. :-)
FYI @raphaelsepulveda :-)
let myArray = [ "Item 1 (remove this)", "Item 2 (remove that)", "Item 3 (remove this also)", "Item but not this one 4", ]; let stringsToRemove = [ "(remove this)", "(remove that)", "(remove this also)", "but not this one", ]; /**@param {string} str */ function trimFromEnd(str, stringsToRemove) { for (let stringToRemove of stringsToRemove) { //Check if str to remove ends with stringToRemove. if (str.endsWith(stringToRemove)) { //It does, so remove it from the end. str = str.substring(0, str.length - stringToRemove.length) } } return str.trim(); } let trimmedStringsArray = myArray.map(s => trimFromEnd(s, stringsToRemove)); log(trimmedStringsArray);
Raphael Sepulveda @raphaelsepulveda2021-01-19 02:11:23.963Z
Nice and concise, love it!
Kitch Membery @Kitch2021-01-19 02:14:46.083Z
So good. Thanks for your reply also, @raphaelsepulveda Much appreciated! :-)
- In reply toKitch⬆:
Dustin Harris @Dustin_Harris
Oh this is gorgeous. I have a bunch of scripts I need to use this in. :)
Kitch Membery @Kitch2021-01-19 03:32:26.563Z
I agree!!
- In reply toKitch⬆:Christian Scheuer @chrscheuer2021-01-19 13:12:01.002Z
Just to follow up.
Think of Regex'es as extremely powerful devices to create advanced pattern matching. But always think of Regex'es as something you manually design. Whenever you start thinking, can I automatically create a Regex from an input string, you're doing it wrong. Never (almost never) create regex'es automatically based on user input. It opens up a huuuuge can of worms and provides much more complexity than it helps 99.9% of cases :)
Regex'es are powerful, but think about how you can do it with regular string and array functions first :)Kitch Membery @Kitch2021-01-19 16:59:31.426Z
Noted... Thanks again @chrscheuer. :-)