最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Javascript replace special chars with empty strings - Stack Overflow

programmeradmin3浏览0评论

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 :)

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 :)

Share Improve this question asked Dec 27, 2010 at 7:31 SoLoGHoSTSoLoGHoST 2,6897 gold badges30 silver badges53 bronze badges 4
  • What's the goal? Sounds like you want to strip tags, i.e. extract the text content from HTML, correct? – deceze Commented Dec 27, 2010 at 7:35
  • No, I am allowing users to type into a text box, and this goes into the database, and the textbox is critical for it's input. It checks if the select box already has this text in it. So it needs to strip special chars because I don't want to insert into the database the same value that is already in there. – SoLoGHoST Commented Dec 27, 2010 at 7:44
  • However DO make sure you keep your php stripping too since it it very easy to bypass your javascript to inject html directly into your database if you do not test on the server – mplungjan Commented Dec 27, 2010 at 7:49
  • Like I said, I already have this part figured out on the PHP side of things. I strip it just fine in PHP. Completely removing all of this. Just the JS part of it is Not being stripped. Anyways, someone answered it. Cheers :) – SoLoGHoST Commented Dec 27, 2010 at 7:56
Add a comment  | 

1 Answer 1

Reset to default 15

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,'');
发布评论

评论列表(0)

  1. 暂无评论