开发者

javascript : how to replace symbol with regex?

how to remove the symbols such a开发者_如何学Cs ▼, >>,<< and others using the regex in javascript?


You can use the replace function for this, specifying the empty string as the replacement string. Here are a couple examples.

If you only want to strip specific characters:

s = s.replace(/[▼><]/g, '');

Or using a Unicode escape sequence:

s = s.replace(/[\u25bc><]/g, '');

If you want to strip all but alphanumeric characters:

s = s.replace(/[^A-Za-z0-9]/, '');

Edit: described Unicode escape sequence usage.


I'd remove non-standard character(s) by using the unicode token \u and the corresponding character code.

For example:

// Remove "▼" using its character code
var s = "I like milk ▼.".replace(/\u9660/g, "");


You can use replace(/[\u0100-\uffff]/g, '') to remove characters outside the extended ASCII range.

E.g.

>>> "I 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜