简体   繁体   中英

Javascript regular expression match string to start with letter or number

I am trying to see if my string starts with a letter or a number. I think I'm close, could anyone help me out?

if(thestring.match("/^[\pL\pN]/"))

Use:

^[A-Z0-9]

With a case-insensitive modifier:

if(thestring.match(/^[A-Z0-9]/i)) {}

Demo


\\pL and \\pN are PCRE shortcodes and do not work in Javascript.

if(/^[a-z0-9]/i.test(thestring)) {
    //do something
}

.test() is much more simple.
It returns just false or true , while .match() resturns null or an array.

More information differences between .test() and .match()

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