Learn Photoshop, Flash, Adobe AIR, ExtJS, jQuery, Ajax, Dojo, HTML, CSS, JavaScript, XML, Accessibility, Database, DWR, Gears, GWT, Java, JSON, MooTools, Office, Perl, PHP, Programming, Prototype, Scriptaculous and more

Want to explore your choice of video from all over world at once place! then what are you waiting for, just click, explore and learn.

Tech Video Bytes

Remove special characters (like !, >, ?, ., # etc.,) from a string using JavaScript

Categories: Programming
Tags: ,
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.

12 Responses to “Remove special characters (like !, >, ?, ., # etc.,) from a string using JavaScript”

  1. AJAY KUMAR Says:

    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]

  2. admin Says:

    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

  3. Jesper O. Jensen Says:

    Hi author

    This was very helpfull.

    Kind regards
    From a danish web delevoper

  4. admin Says:

    Hi Danish

    Thanks for this.

  5. flash Saat Says:

    html special characters —
    http://html-lesson.blogspot.com/2008/06/special-characters.html

  6. Css Dersleri Says:

    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

  7. Mahendra Says:

    Hi,
    I really needed this code thanks.

    Mahendra

  8. Developer Says:

    Nice work. worked on the first run.

  9. RD Says:

    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.

  10. Sasa Says:

    Thank You!

    Works great - one problem solved :)

  11. khp Says:

    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

  12. khp Says:

    Yes working..Thank you

Leave a Reply

Featured & Popular Articles