开发者

delete some special character from a string by javascript

开发者 https://www.devze.com 2023-01-04 06:07 出处:网络
How we can delete some special character from a string 开发者_Python百科by javascriptThe best way is to replace it, either using a string or regular expression.

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

精彩评论

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