6

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
  • 8,879
  • 18
  • 83
  • 166

2 Answers2

11

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.

Sam
  • 20,096
  • 2
  • 45
  • 71
5
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()

Community
  • 1
  • 1
Al.G.
  • 4,327
  • 6
  • 31
  • 56