Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript replace special chars with empty strings

Ok, I have these string prototypes to work with, however, I don't understand what they do exactly.

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, what I need to do is replace all instances of double quotes ("), >, <, single quotes ('), etc. etc.. Basically the same stuff that htmlentities() in php changes, but I need to replace them with an empty string, so that they are removed from the text.

Can I use any of the functions above? If not, how can I accomplish this in Javascript? Can I use a replace on the string?

Please, someone, help me here. I am placing this text into a select box and will be inputted into the database upon submitting of the form. Though, I am using PHP to remove all of these characters, however, I'm having difficulty finding a way to do this in Javascript.

Thanks :)

like image 437
SoLoGHoST Avatar asked Dec 27 '10 07:12

SoLoGHoST


People also ask

How do you replace a special character in a string?

Using 'str.replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.

What can I use instead of replaceAll in JavaScript?

3.1 The difference between replaceAll() and replace() If search argument is a string, replaceAll() replaces all occurrences of search with replaceWith , while replace() only the first occurence. If search argument is a non-global regular expression, then replaceAll() throws a TypeError exception.

Does JavaScript have replaceAll?

The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match.

What is .replace in JavaScript?

The replace() method searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced. The replace() method does not change the original string.


1 Answers

Remove special characters (like !, >, ?, ., # 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 don't want to remove dot(.) from string:

temp =  temp.replace(/[^a-zA-Z 0-9.]+/g,'');
like image 138
Naveed Avatar answered Sep 27 '22 21:09

Naveed