Javascript regular expression match string to start with letter or number

13,875

Solution 1

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.

Solution 2

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()

Share:
13,875
bryan
Author by

bryan

Updated on June 04, 2022

Comments

  • bryan
    bryan almost 2 years

    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]/"))
    
  • bryan
    bryan almost 10 years
    Hmm the demo work's great, but the javascript doesn't.
  • bryan
    bryan almost 10 years
    Thank you sir. I appreciate the help.