Skip to content

Instantly share code, notes, and snippets.

@srkama
Created February 9, 2016 10:51
Show Gist options
  • Select an option

  • Save srkama/a86d729a67cfaca62d1d to your computer and use it in GitHub Desktop.

Select an option

Save srkama/a86d729a67cfaca62d1d to your computer and use it in GitHub Desktop.
a global replace using regex in javascript
function convert(str) {
// :)
htmlEntities = [
['&', '&'],
['<','&lt;'],
['>', '&gt;'],
['\'','&apos;'],
['"','&quot;'],
];
myRegEx = /[^a-z\d]/i;
for (i=0;i<htmlEntities.length;i++) {
str = str.replace(new RegExp(htmlEntities[i][0], 'g'), htmlEntities[i][1]);
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment