Home > Blockchain >  how to remove words start with # from string
how to remove words start with # from string

Time:05-08

i have title like this "hello my #name is #ahmed kotsh" and i want to remove #name #ahmed , any word start with # and the extra spaces after delete the hashtag

expect result "hello my is kotsh" my try works but i know there is better code and more cleaner

const text = "hello my #name is #ahmed kotsh";
  const arr = text.split(" ");
  let newText = "";
  arr.map((i) => {
    if (i[0] != "#") newText = newText   i   " ";
  });
  console.log(newText);

CodePudding user response:

Use a regular expression that matches # followed by non-space characters, and replace with nothing.

const text = "hello my #name is #ahmed kotsh";
console.log(
  text.replace(/#\S  ?/g, '')
);

  • Related