0

I have a html entities replacement for & that look like this:

function htmlEntities(str) {
return String(str).replace(/&(?!amp;)/g, '&');
}

which are working fine that not to replace the & but will replace &

how do I add multiple condition to regex of my function so that it will not mess with other html entities like:

'
"
>
<

i test with:

var xxx = "1234 &aaa&amp; aaadas 'xxx' \" kkk <aasd> xxxxxxxxxxxxxxxx &lt;";

console.log(htmlEntities(xxx));

it will replace the &lt; to become &amp;lt; and this is not what I want, i need it to leave the &lt; untouch just like the example &aaa&amp; to become &amp;aaa&amp;

hope you get what I mean, any idea?

2

1 Answer 1

1

You can use | in a regexp to make alternatives.

var xxx = "1234 &aaa&amp; aaadas 'xxx' \" kkk <aasd> xxxxxxxxxxxxxxxx &lt;";
console.log(htmlEntities(xxx));

function htmlEntities(str) {
  return String(str).replace(/&(?!(?:amp|apos|gt|lt);)/g, '&amp;');
}

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.