JS replaceAll matching regex with actual string content

Viewed 15

I have made a regex expression which matches all hashtags:

/\#([\w\-]+)/g

I want to replace hashtags with <HASHTAG>. I do:

  var x = "#hello my name is"
  x.replaceAll(/\#([\w\-]+)/g, "<HASHTAG>")
  console.log(x) //<HASHTAG> my name is

And this works as expected. How would I go about replacing all substrings which satisfy my regex with <HASHTAG:actual hashtag> instead of only <HASHTAG>?

In the example above, I would like to obtain:

<HASHTAG:hello> instead of <HASHTAG>

0 Answers
Related