The best way in my opinion is to use the browser's inbuilt HTML escape functionality to handle many of the cases. To do this simply create a element in the DOM tree and set the innerText of the element to your string. Then retrieve the innerHTML of the element. The browser will return an HTML encoded string.

function HtmlEncode(s)
{
  var el = document.createElement("div");
  el.innerText = el.textContent = s;
  s = el.innerHTML;
  return s;
}

Test run:

alert(HtmlEncode('&;\'><"'));

Output:

&amp;;'&gt;&lt;"

This method of escaping HTML is also used by the Prototype JS library though differently from the simplistic sample I have given.

Note: You will still need to escape quotes (double and single) yourself. You can use any of the methods outlined by others here.

Answer from Cerebrus on Stack Overflow
🌐
GitHub
github.com › PatricNox › SpecialToNormal
GitHub - PatricNox/SpecialToNormal: Replace special characters to normalized letters in JavaScript
You have to convert special characters in huge chunks of strings · You want to validate a very large text · $ npm install specialtonormal · or · $ yarn add specialtonormal · import normalizeSpecialCharacters from "specialtonormal"; const ...
Author   PatricNox
🌐
Metring
ricardometring.com › articles › javascript-replace-special-characters
JavaScript: Replacing Special Characters - The Clean Way
April 12, 2019 - The normalize method was introduced in the ES6 version of JavaScript in 2015. It serves to convert a string into its standard Unicode format.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-special-characters-to-html-in-javascript
How to Convert Special Characters to HTML in JavaScript? | GeeksforGeeks
October 10, 2024 - One another way is to convert each special character to its respective HTML code using javascript. Within the script we will replace all the special charters with the help of a regular expression which is "&#" + ASCII value of character + ";".
🌐
The Art of Web
the-art-of-web.com › javascript › escape
Escaping Special Characters < JavaScript | The Art of Web
The htmlentities function escapes characters which have special meaning inside HTML by inserting HTML entities in their place (eg. &amp; in place of &). See our article on ASCII Character Codes for more details. All functions have a complementary 'decode' function that pretty much does the opposite. Another essential PHP function that comes in handy when passing data to JavaScript is addslashes which will add a backslash before: backslashes, single- and double-quotes.
🌐
npm
npmjs.com › package › replace-special-characters
replace-special-characters - npm
October 17, 2022 - To prevent this validation from being done manually, our lib is able to do it together with the normalization of the text. ... const replaceSpecialCharacters = require('replace-special-characters'); const normalizedString = replaceSpecialCharacters('JäváSçrîpt'); //=> 'JavaScript'
      » npm install replace-special-characters
    
Published   Oct 17, 2022
Version   1.2.7
Author   Roberto Alves
🌐
Stack Overflow
stackoverflow.com › questions › 16438751 › transfer-special-charcter-to-normal-charcter
javascript - transfer special charcter to normal charcter - Stack Overflow
// create a character map to convert one char to another var charMap = { "ā" : "a", "ū" : "u" }; var region="Ahmadābād,Sūrat,Vadodara,Rājkot,Bhāvnagar,Jāmnagar,Nadiād,Gāndhīnagar,Jūnāgadh,Surendranagar"; // split original string into char array var chars = region.split(''); // init new array for conversion result var charsConverted = []; // convert characters one by one for(var i = 0; i < chars.length; i++){ var char = chars[i]; // this will try to use a matching char from char map // will use original if no pair found in charMap charsConverted.push( charMap[char] || char); } // join array to string var result = charsConverted.join(''); alert(region); alert(result);
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-special-characters-to-html-in-javascript
How to convert special characters to HTML in Javascript?
February 21, 2023 - We can use replace() method to convert special characters to HTML in JavaScript. This method can also be applied with a map. There are many other methods that can be used for this task. We will discuss these methods in detail in this article. We will
Find elsewhere
🌐
Plunker
embed.plnkr.co › plunk › 06dJ3A
JavaScript Regular Expression to Replace Special Characters from String - Plunker
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>JavaScript Regular Expression to Replace Special Characters from String</title> <link rel="stylesheet" href="style.css" /> <script data-require="jquery" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script> <script src="script.js"></script> <script type="text/javascript"> function replaceCharacters() { var regExpr = /[^a-zA-Z0-9-. ]/g; var userText = document.getElementById('user').value; alert(userText.replace(regExpr, "")); } </script> </head> <body> <div> <h1>JavaScript Regular Expression to Replace Escap
🌐
CoreUI
coreui.io › answers › how-to-remove-special-characters-from-a-string-in-javascript
How to remove special characters from a string in JavaScript · CoreUI
May 20, 2026 - Use replace() with regular expressions to efficiently remove special characters from strings in JavaScript.
🌐
Amit Merchant
amitmerchant.com › replace-accented-characters-with-plain-english
Replace Accented Characters with their Plain English Counterparts in JavaScript
July 12, 2023 - As you can tell, we’re using the regex /[\u0300-\u036f]/g to match all the accented characters and replace them with an empty string. We can even make this into a reusable function like so. const replaceAccentedCharacters = (str) => { return str.normalize('NFD') .replace(/[\u0300-\u036f]/g, ''); } And that’s how you can replace accented characters with their plain English counterparts in JavaScript.
🌐
SmartBear
support.smartbear.com › testcomplete › docs › scripting › working-with › strings › javascript.html
JavaScript - Working With Strings | TestComplete Documentation
To deal with strings, TestComplete has a special aqString scripting object. The object is available for all supported scripting languages, so that you can use it to operate with string values regardless of the chosen language. Another scripting object that is useful for string manipulation is aqConvert. This object has several methods that convert values of different types to a string representation and vice versa. Furthermore, JavaScript has its own inherent object String, that is, a wrapper for a string data type.
🌐
John Kavanagh
johnkavanagh.co.uk › home › articles › escaping and unescaping special characters in javascript
Escaping and Unescaping Special Characters in JavaScript
July 6, 2018 - This is really important when dealing ... Let's talk through some common scenarios. In JavaScript, you can escape characters using a backslash (\)....
🌐
SitePoint
sitepoint.com › javascript
Efficient way of replacing special characters - JavaScript - SitePoint Forums | Web Development & Design Community
April 9, 2024 - Suppose I have a string like this: This is a fridge with 10 chocolates and ~5 icecream samples description to check if backward slash \ breaks anything and checking caret ^ symbol as well I want to replace those special characters with the following: \ to \1F ~ to \7E ^ to \5E I may find more to replace in future but at this point I just want to handle above such that after replacing it, the string looks like the following: This is a fridge with 10 horse and \7E5 goat samples descriptio...
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › string › remove accents
Remove accents from a JavaScript string - 30 seconds of code
February 7, 2024 - This might seem like a difficult task, but it's actually quite simple. As a matter of fact, JavaScript's String.prototype.normalize() method makes it a breeze, given its 'NFD' option.
🌐
W3Schools
w3schools.com › jsref › jsref_unescape.asp
JavaScript unescape() Method
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
GitHub
gist.github.com › hawkapparel › dec53846ddeb593147ad21fda6b4383c
Convert special characters to normal · GitHub
Convert special characters to normal · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·