1
function escapeHtml(text) {

      return text
        .replace(/\t/g, "")
        .replace(/\n/g, "")
        .replace(/%/g,"")
        .replace(/\s/g, " ")
        .replace(/&/g, "")
        .replace(/</g, "")
        .replace(/>/g, "")
}

can somebody provide me the regex for all the above entities into a single regex?

3
  • 1
    Can you create example you want, I think your method wrong '<div>something</div>' -> 'divsomething/div' Commented Sep 4, 2018 at 4:09
  • 1
    Did you really mean to replace \s with a space, instead of with an empty string like all the rest? Commented Sep 4, 2018 at 4:13
  • \s is the same as space, \t, and \n. but you already replaced \t and \n earlier. Commented Sep 4, 2018 at 4:14

2 Answers 2

4

If \s is being replaced with a space, while all the others are being replaced with the empty string, it's not possible with a single regex unless you provide it with a replacer function, which doesn't make much sense - just use two .replaces. To be concise, use a character set:

return text
  .replace(/[\t\n%&<>]/g, '')
  .replace(/\s/g, ' ');
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

text.replace(/[&%$<>\t\s]/g,"");

replace all enter, whitespaces & tabs with \s and \t and character &%$<>

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.