Arpan
Arpan

Reputation:

delete some special character from a string by javascript

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

Upvotes: 1

Views: 2007

Answers (2)

Wind Chimez
Wind Chimez

Reputation: 1176

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.

Upvotes: 0

Kerry Jones
Kerry Jones

Reputation: 21838

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!"

Upvotes: 5

Related Questions