how to find specific special character from string and replace it to unicode \u0026 + = \u002B
replace only these special character : [$, &, +, #]
example : "HELLO JAMES (WITH ME YOUR) \n+++ SEE & FIELD 4-B +++
MY code
var char = '+';
var saa =char.charCodeAt(0);
console.log(saa)
var codeHex = saa.toString(16).toUpperCase();
while (codeHex.length < 4) {
codeHex = "0" + codeHex;
}
var afteruni = name.replaceAll('\+','\\u'+codeHex)
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
i want like this :
"HELLO JAMES (WITH ME YOUR) \n\u002B\u002B\u002B SEE \u0026 FIELD 4-B \u002B\u002B\u002B"
ERROR : Invalid regular expression: /+/: Nothing to repeat\
you don't need to redefined replaceAll - why do that? replaceAll will already give you a correct response :
just replace line: var afteruni = name.replaceAll('\+','\\u'+codeHex)
with var afteruni = name.replaceAll(/\+/g,'\\u'+codeHex)
iv'e run that in my PC and it is working.