html encode string js code example

Example 1: javascript html encode

function htmlEntities(s){
	return s.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
  		return '&#' + i.charCodeAt(0) + ';';
	});
}

Example 2: how to encode a string in javascript

var uri = "my test.asp?name=ståle&car=saab";
var res = encodeURI(uri);

Example 3: javascript html special characters

return mystring.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");

Example 4: jq unencode string

# -r enable raw output

jq -r .[0].string FILE.json