1
let message ="hello https://ggogle.com parul https://yahoo.com and http://web.com";
let url=["https://ggogle.com", "https://yahoo.com", "http://web.com"];

I want to put text "***" in place of all url found in the message array from url array.

Tried this way -

let message = "hello https://ggogle.com parul https://yahoo.com and http://web.com";
let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"];
const replace = url.map(item => {
  let indexes = message.indexOf(item);
  let output = message.replaceAt(indexes, "***")
})

Error-message.replace is not a function,also if the logic is correct? Thanks!

1
  • 1
    You're not returning anything from your map callback. Did you mean forEach? map returns a new array, forEach work in-place. Commented Sep 22, 2021 at 14:04

3 Answers 3

1

1) You can easily achive the result using reduce

let message =
  "hello https://ggogle.com parul https://yahoo.com and http://web.com";

let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"];

const result = url.reduce((msg, str) => msg.replace(new RegExp(str, "g"), "***"), message);

console.log(result);

2) You can also achieve the solution with forEach loop

let message =
  "hello https://ggogle.com parul https://yahoo.com and http://web.com";

let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"];

url.forEach((s) => (message = message.replace(new RegExp(s, "g"), "***")));

console.log(message);

3) Using replace

let message =
  "hello https://ggogle.com parul https://yahoo.com and http://web.com";

let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"];

const result = url.reduce((msg, str) => {
  while (msg.indexOf(str) !== -1) msg = msg.replace(str, "***");
  return msg;
}, message);

console.log(result);

Sign up to request clarification or add additional context in comments.

6 Comments

good solution))
@HR01M8055 thank you, is there a way that I don't have use regx?
You can but that would be hard to understand and maintain as compared to RegExp
This solution is direct and efficient. But at the end of day you have to replace the string with some text and you gonna use replace
Though I've added one more solution. But still I'd prefer either 1 or 2
|
0

The error is correct. replaceAt isn't a function, you're gonna have to use something like string substr() and then add your string inside, unless you think the string replace() method is good for your situation. I don't know enough about your case to decide.

This question might be useful, but make sure you actually add in the "***" after you cut out part of the string.

Comments

0

If I understood you correctly, you can just use replace instead of replaceAt. For example:

let message = "hello https://ggogle.com parul https://yahoo.com and http://web.com";
let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"];
const replace = url.map(item => {
    message = message.replace(item, "***")
})

console.log(message);

2 Comments

If you are not using the return value of map then It would be better to use forEach
What if the input is "hello https://ggogle.com parul https://yahoo.com and http://web.com and https://ggogle.com". Then your snippet is not as expected...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.