1

I am working on a Twitter program and am stuck on how to replace words with html elements. For example:

Replace: Hello #World, This is a Tweet!

With: Hello <a href="">#World</a>, this is a Tweet!

I know that you can use regex in replace() but I am unsure of how to reference to the matched string after in the second argument of replace()

Thank you!

1 Answer 1

3

You can use this regular expression

str="Hello #World"
str.replace(/#(\S*)/g,'<a href="">$1</a>');
Sign up to request clarification or add additional context in comments.

Comments

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.