0

Below is my code.

var input = "this is @renish profile";
var matches = input.replace(/(@\w*)/g,'<a href="http://example.com/users/profile/$1">$1</a>');

The output is:

this is <a href="http://example.com/users/profile/@renish">@renish</a> profile

This is print html code. How do I convert a tag from string?

3
  • What are you trying to achieve? Commented Jul 2, 2015 at 14:27
  • If you are using jQuery you can use jQuery.parseHTML() api.jquery.com/jquery.parsehtml Commented Jul 2, 2015 at 14:29
  • 2
    How are you currently adding that string to the page.... That is the $1,000 question. Commented Jul 2, 2015 at 14:31

1 Answer 1

2

create a div, and use innerHTML.

var input = "this is @renish profile";
var matches = input.replace(/(@\w*)/g,'<a href="http://example.com/users/profile/$1">$1</a>');

var div = document.createElement('div');
div.innerHTML = matches;

document.body.appendChild(div)

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.