-1

I want to check if a given string's first character is a word character/not a digit or white-space and return a boolean.

I'm trying to use regex for this. What I have is:

function isFirstIndexCharacter(str){

   var regex = RegExp('w')
   return regex.test(str[0]);
} 


I always get false no matter what my input is. Thanks for any help on this.

mangokitty
  • 389
  • 1
  • 2
  • 11
  • `var regex = /^\w/;` – Pointy Oct 23 '19 at 19:59
  • 3
    That's just checking that the character `w` is present. For a word character use`^\w` and for a word character (without digit) use `^[^\W\d]`. You should note that `\w` also includes `_` so if that's not what you're looking for, use `^[a-zA-Z]` – ctwheels Oct 23 '19 at 19:59
  • check [this](https://stackoverflow.com/questions/1496826/check-if-a-single-character-is-a-whitespace). It doesn't include numbers, but numbers is the easy part... – Leonardo Alves Machado Oct 23 '19 at 20:01

1 Answers1

2

You can do something like this -

function isFirstIndexCharacter(str){
  return /^\w/.test(str[0]);
} 
Arik
  • 3,779
  • 1
  • 18
  • 20