1

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

2 Answers 2

5

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!"
Sign up to request clarification or add additional context in comments.

3 Comments

if i want to remove h, o & w then.
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.
@Arpan Steve314 is right, though I've never seen the .replace used directly on a string. /[how]/ is the regular expression you want.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.