简体   繁体   中英

How to make a group of regex characters optional?

I'm trying to create a regex that allows unicode letters, digits, -, and apostrophes where the first character is a letter or number, while subsequent characters can be letters, numbers, -, or '. I think that my regex works fine except in the case where a user enters a single letter or number. Is there anyway to make my 2+ characters to be optional? Below is my current regex:

/^[\p{L}0-9]+[-\'\p{L}0-9']+$/u

Thanks!

-Eric

Without using ? you can use:

/^[\p{L}0-9]+[-\'\p{L}0-9']*$/u

to allow single alpha-numeric in input since [-\\'\\p{L}0-9']* means 0 or matches.

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