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!

pilchard
  • 12,414
  • 5
  • 11
  • 23
Rockstar
  • 11
  • 1
  • 1
    You're not returning anything from your `map` callback. Did you mean `forEach`? `map` returns a new array, `forEach` work in-place. – Andy Sep 22 '21 at 14:04

3 Answers3

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);
DecPK
  • 24,537
  • 6
  • 26
  • 42
  • good solution)) – Vitaliy Rayets Sep 22 '21 at 14:10
  • @HR01M8055 thank you, is there a way that I don't have use regx? – Rockstar Sep 22 '21 at 14:16
  • You can but that would be hard to understand and maintain as compared to `RegExp` – DecPK Sep 22 '21 at 14:21
  • 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` – DecPK Sep 22 '21 at 14:24
  • Though I've added one more solution. But still I'd prefer either `1` or `2` – DecPK Sep 22 '21 at 14:26
  • Would you mind If you can [upvote](https://stackoverflow.com/help/privileges/vote-up#:~:text=How%20do%20I%20vote%20up,arrow%20to%20undo%20a%20downvote.) and [accepting](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) my answer if it solved your issue? – DecPK Sep 29 '21 at 10:12
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.

DexieTheSheep
  • 439
  • 1
  • 6
  • 16
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);
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Mor M Ben
  • 21
  • 4
  • If you are not using the return value of `map` then It would be better to use `forEach` – DecPK Sep 22 '21 at 14:27
  • 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... – DecPK Sep 22 '21 at 14:29