I wish to allow both upper case and lower case. I have tried
'k abBcdi #!129'.replace(/^[A-Za-z0-9]/g,'')
But it's not give me the correct answer
您需要在方括号内使用NOT运算符( ^
):
/[^A-Za-z0-9]/g
Also use [^0-9A-z] instead. It probably has little to no performance effect, but it is slightly shorter and prettier.
Edit:Per a comment above, are you trying to find all letters and numbers and replace them or remove everything that's not a letter and number?
If you don't explicitly need to use the not ^
operator, you could simply use a special character to identify all non-alphanumeric characters:
'k abBcdi #!129'.replace(/[\W]/g,'')
Or, given that \\W
allows the underscore ( _
) it might be preferred to use:
'k abBcdi #!129'.replace(/(\W+)|(_)/g,'')
References:
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.