tags:

views:

47

answers:

4

How we can delete some special character from a string by javascript

+5  A: 

The best way is to replace it, either using a string or regular expression.

String:

// JavaScript Document
var string = 'Hello world!';
alert( string.replace( 'world', '' ) ); // Alerts "Hello !"  

Regular Expression:

// JavaScript Document
var string = 'Hello world!';
alert( string.replace( /o/, '' ) ); // Alerts "Hell wrld!"
Kerry
If you want to remove all occurences of h, o and w, use a regular expression that matches for any of those three characters. To do that, wrap the characters in square brackets - e.g. alert ('hello world!'.replace (/[how]/, '')); - I think that's right for Javascript.
Steve314
@Arpan Steve314 is right, though I've never seen the `.replace` used directly on a string. /[how]/ is the regular expression you want.
Kerry
A: 

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

http://www.developersnippets.com/2007/05/12/remove-special-characters-like-etc-from-a-string-using-javascript/

Remove special characters from a string using JavaScript

http://digg.com/programming/Remove_special_characters_from_a_string_using_JavaScript

ratty
A: 

Well,

if the string to get cleaned up is mystring;

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

will remove all charectes other than alpahnumeric from your string. You can modify the regular expression accordingly if you wan tot exclude some special characters from cleaning up.

Wind Chimez
A: 

Well,

if the string to get cleaned up is mystring;

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

will remove all charectes other than alpahnumeric from your string. You can modify the regular expression accordingly if you wan tot exclude some special characters from cleaning up.

Wind Chimez