简体   繁体   中英

Remove some characters from a string in javascript

The characters in my string need to match this expression:

^[a-zA-Z0-9_-:]+$,

I want to remove every character that does not match the expression above (also spaces).

How can I achieve this?

Replace is your friend here:

var str="asfafasf@##f2f32f";
str.replace(/[^a-zA-Z0-9_\-:]/g,"") //output: asfafasff2f32f

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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