'how to remove words start with # from string
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);
Solution 1:[1]
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, '')
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | CertainPerformance |