Remove special characters (like !, >, ?, ., # etc.,) from a string using JavaScript
Categories: Programming
Tags: HTML, JavaScript
Written By: admin
Hi Guys, I would like to share a JavaScript snippet code which will remove special characters (like !, >, ?, ., # etc.,) from a string. Well - Recently I was working on a project where in which I want to remove some special characters from a given string or user entered string. Well for that I have written a JavaScript Code but it has taken 10 lines of code, I thought whether this is feasible and right way of practice, one of my friend has suggested to use Regular Expressions instead.
While surfing, I have downloaded a tutorial to learn Regular Expressions in JavaScript. It has helped me a lot - frankly to say - yes! there is a power in Regular Expressions. As I have explained above - for removing special characters from a String I have written 10 lines of code but after using Regular Expressions it has taken only 1 line of code - isn’t great.
Here is the code for removing special characters (like !, >, ?, ., # etc.,) from a string using JavaScript:
<script language="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>');
//--></script>
Please comment on the same, whether the above snippet is helpful to you guys.








July 2nd, 2007 at 8:39 pm
hi,
i hav used ur coading in firefox it is not working can u tel me the reason. Do i need to add any other code.
thx & rgds,
Ajay.
the following is my code…………
[CODE]
html>
function splrml(stringval){
alert(’hi’);
temp = stringval.replace(/[^a-zA-Z 0-9] /g,”);
document.write(”hi”);
}
[/CODE]
July 2nd, 2007 at 10:12 pm
Hi Ajay,
I think you have just copied the code which I have placed in my snippet, if you look at the code properly, while posting this article I have placed ‘ - (single quote - in keyboard - the key which is next to : but the browser has rendered in some other fashion like single quotation - if you want the above code to work in any browser, just copy the code and paste it in any tool like if you are using Dreamweaver, EditPlus, TextPad - just you need to do some necessary changes like I have mentioned above - single quotes, double quotes etc.,
Try this out, it will work definately (or else I will assist you in this regard).
Please let me know, Am I answered your question.
Thanks,
Admin
May 14th, 2008 at 9:54 am
Hi author
This was very helpfull.
Kind regards
From a danish web delevoper
May 15th, 2008 at 5:14 am
Hi Danish
Thanks for this.
June 17th, 2008 at 5:00 pm
html special characters —
http://html-lesson.blogspot.com/2008/06/special-characters.html
July 5th, 2008 at 3:05 pm
HI i need your help i really want to create my own website/web page but i dont know how to go about doing it so can you please help me out
August 14th, 2008 at 1:26 pm
Hi,
I really needed this code thanks.
Mahendra
August 19th, 2008 at 11:31 am
Nice work. worked on the first run.
October 1st, 2008 at 2:31 pm
Hi,
This was really helped. Modified the Reg Exp a bit so it could be used in my Java code.
regex = “[^a-zA-Z0-9]“;
unid = unid.replaceAll(regex,”");
Thanks.
November 4th, 2008 at 11:31 am
Thank You!
Works great - one problem solved
November 26th, 2008 at 5:28 am
I changed the text to
ring’s… So??? What…’);
document.write(temp + ”);
temp = temp.replace(/[^a-zA-Z 0-9]+/g,”);
document.write(temp + ”);
//–>
iam trying to replace the single quote also in “string’s”
help me
khp
November 26th, 2008 at 5:57 am
Yes working..Thank you