How do I replace multiple strings in an array of Items

Dear Javascript legends of freeCodeCamp!

I need your help!

I have this code;

let myArray = ['he123llo', 'cats', 'wor123ld', 'dogs'];

const result = myArray.map(x =>x
    .replace('1', '')
    .replace('2', '')
    .replace('3', '')
    .replace('s', '') 
);
console.log(result); //[ 'hello', 'cat', 'world', 'dog' ]

It works great however I’d prefer to have two arrays like this;

let myArray = ['he123llo', 'cats', 'wor123ld', 'dogs'];
let replaceStrings = ['1','2','3','s'];

Then feed them into a function that accepts two arguments and returns the same result as the above script.

Any help would be awesome!

Thanks in advance.

Use a regex, not an array, and apply it to each string. Example:

/**
 * @param {string[]} strings
 * @param {RegExp} targetChars
 */
function removeSpecifiedChars(strings, targetChars) {
  return strings.map((string, i) => src.replace(targetChars, ""));
}

So like

removeSpecifiedChars(myArray, /[123s]/g)

Or with two arrays:

removeSpecifiedChars(myArray, RegExp(`[${replaceStrings.join("")}]`, "g"));

Or to allow two arrays as arguments:

/**
 * @param {string[]} strings
 * @param {string[]} targetChars
 */
function removeSpecifiedChars(strings, targetChars) {
  const replaceReg = RegExp(`[${targetChars.join("")}]`, "g");

  return strings.map((string, i) => src.replace(replaceReg, ""));
}
1 Like

Dan,

Thank you so much for your reply.

I prrobably should have mentioned that I want to use strings longer than a single character. like this;

let myArray = [
  "Item 1 remove this",
  "Item 2 remove that",
  "Item 3 remove this also",
  "Item and this one as well 4 ",
];


let replaceStrings = [
  " remove this",
  " remove that",
  " remove this also",
  " and this one as well ",
];

And return

[
  "Item 1",
  "Item 2",
  "Item 3",
  "Item 4 ",
];

Also I see that your code uses “src”, and I get an error "src is not defined’. What would I use in its place if I am wanting to use the script in a non html/ web environment.

I’ll have to do some homework on the ‘src’ property.

Thanks again!

Sorry, it’s not a property, I’ve just mistyped something – it’s the callback function’s argument, I originally called it src.

Anyway, just use | instead of [], cf

/**
 * @param {string[]} strings
 * @param {string[]} targetStrings
 */
function removeSpecifiedStrings(strings, targetStrings) {
  const replaceReg = RegExp(targetStrings.join("|"), "g");

  return strings.map((string, i) => src.replace(replaceReg, "");
}
1 Like

Awesome. Thank you Dan.

This is wha I have so far.

let myArray = [
  "Item 1 remove this",
  "Item 2 remove that",
  "Item 3 remove this also",
  "Item and this one as well 4",
];

let replaceStrings = [
  " remove this",
  " remove that",
  " remove this also",
  " and this one as well",
];

/**
 * @param {string[]} strings
 * @param {string[]} targetStrings
 */
function removeSpecifiedStrings(strings, targetStrings) {
  const replaceReg = RegExp(`${targetStrings.join("|")}`, "g");

  return strings.map((string, i) => string.replace(replaceReg, ""));
}

let myResult = removeSpecifiedStrings(myArray, replaceStrings);

console.log(myResult); // [ 'Item 1', 'Item 2', 'Item 3 also', 'Item 4' ]

My logged result is not getting rid of " also"

I assume this is a regex thing.

Thoughts?

Put the ones that are longer first: the issue you have is that you have " remove this" then " remove this also". Regex pattern matches, left to right. And because " remove this" comes first, that matches, and the replace occurs. If you have [say] two potential matches and they are very similar, the difference being that one has some extra characters, put the longer one first.

tl/dr always match from most specific to least specific

1 Like

Thanks you Dan!

Of course, how did I not see that !! :slight_smile:

Rock on

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.