简体   繁体   English

用javascript从字符串中删除一些特殊字符

[英]delete some special character from a string by javascript

我们如何通过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; 如果要清理的字符串是mystring;

mysring = mystring.replace(/[^a-zA-Z 0-9]+/g,''); 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. 如果要清除某些特殊字符,则可以相应地修改正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM