Skip to content

Instantly share code, notes, and snippets.

@bartcis
Created May 27, 2019 15:05
Show Gist options
  • Select an option

  • Save bartcis/a99ebf3a8859e9cfbdea69f467a18c29 to your computer and use it in GitHub Desktop.

Select an option

Save bartcis/a99ebf3a8859e9cfbdea69f467a18c29 to your computer and use it in GitHub Desktop.
HTML entities converter - solution 2
function convertHTMLInt(string) {
// 1. Use build in replace method and RegExp to change characters
return string
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
}
console.time('Start Algo 2');
console.log(convertHTMLInt(`>I love 'Rock & Roll'<`));
console.timeEnd('Start Algo 2'); // Start Algo 2: 0.35009765625ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment