const htmlEntities = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }; return str.replace(/([&<>"'])/g, match => htmlEntities[match]); }