开发者

delete some special character from a string by javascript

How we can delete some special character from a string 开发者_Python百科by javascript


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜