1

I have this jQuery code which replaces some HTML character codes with their equivalent:

$('.someText').html()
              .replace(/&/g, '&')
              .replace(/&lt;/g, '<')
              .replace(/&gt;/g, '>');

That works and all, but is there a way to combine those replace operations all into one regex to make the code more concise?

I'm still wrapping my head around regular expressions

0

2 Answers 2

2

This question has a solution that is more flexible, in case you ever expand beyond just the basic three entities:

Converting sanitised html back to displayable html

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the find, Domenic! That will come in very handy
well, at least it solved the problem. But it did not answer the question asked. :D
2

I answered a similar question a while ago. Simple answer; there may be a way to do it, but it's probably better to do what you have here. Each replacement is it's own operation, so might as well leave it that way.

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.