Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - check if string starts with

Tags:

javascript

I am trying to check if a string starts with the character: /

How can i accomplish this?

like image 763
Alosyius Avatar asked Nov 27 '22 09:11

Alosyius


2 Answers

if(someString.indexOf('/') === 0) {
}
like image 165
Justin Niessner Avatar answered Dec 09 '22 18:12

Justin Niessner


Characters of a string can be accessed through the subscript operator [].

if (string[0] == '/') {

}

[0] means the first character in the string as indexing is 0-based in JS. The above can also be done with regular expressions.

like image 24
David G Avatar answered Dec 09 '22 19:12

David G