简体   繁体   中英

In JavaScript, how do I change my regular expression to match all non-alphanumeric characters?

My code is as follows:

"File name) - Title".match("[^ a-zA-Z\d\s:]")

At the moment, it matches ")" but I want it to match ") -", being the non alpha-numeric non-space characters between 'File name' and 'Title'

How do I change my regex to do this?

If you want to match ")" and "-" separately, use g flag

"File name) - Title".match(/[^ a-zA-Z\d\s:]/g)

If you want to match ") -" which is non-alphanumeric+space+non-alphanumeric,

"File name) - Title".match(/[^ a-zA-Z\d\s:]( )*[^ a-zA-Z\d\s:]/g)
/\s*(?:[^a-zA-Z\d\s:]\s*)+/

This regular expression matches optional leading spaces that are followed by one or more groups of non alphanumeric characters and optional trailing spaces. The question mark just means that what is captured within the round brackets is not saved as special component

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