Javascript replaces special characters with blank lines

Well, I have these string prototypes for work, however I don't understand what they do for sure.

String.prototype.php_htmlspecialchars = function() { return this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'); } String.prototype.php_unhtmlspecialchars = function() { return this.replace(/&quot;/g, '"').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&'); } String.prototype.php_addslashes = function() { return this.replace(/\\/g, '\\\\').replace(/'/g, '\\\''); } String.prototype._replaceEntities = function(sInput, sDummy, sNum) { return String.fromCharCode(parseInt(sNum)); } String.prototype.removeEntities = function() { return this.replace(/&(amp;)?#(\d+);/g, this._replaceEntities); } String.prototype.easyReplace = function (oReplacements) { var sResult = this; for (var sSearch in oReplacements) sResult = sResult.replace(new RegExp('%' + sSearch + '%', 'g'), oReplacements[sSearch]); return sResult; } 

Basically, I need to replace all instances of double quotes ("),>, <, single quotes ('), etc. etc. Basically the same things as htmlentities () in php, but I need replace them with an empty string so that they are removed from the text.

Can any of the above functions be used? If not, how can I do this in Javascript? Can I use substitution in a string?

Please someone help me here. I put this text in the selection box and will be entered into the database when the form is submitted. Although, I use PHP to remove all of these characters, however, I find it difficult to find a way to do this in Javascript.

Thanks:)

+4
source share
1 answer

Remove special characters (e.g. !,>,?,., #, Etc.) from a string using JavaScript:

 var temp = new String('This is a te!!!!st st>ring... So??? What...'); document.write(temp + '<br>'); temp = temp.replace(/[^a-zA-Z 0-9]+/g,''); document.write(temp + '<br>'); 

jsFiddle

Edit:

If you do not want to remove the period (.) From the line:

 temp = temp.replace(/[^a-zA-Z 0-9.]+/g,''); 
+10
source

Source: https://habr.com/ru/post/1333451/


All Articles