I want to check is the first letter of variable a (! - Exclamation ) Have any here an example for me ? i found other things but not for first letter is exclamation. thanks so much for reply me
Asked
Active
Viewed 77 times
-5
-
Are you trying to write regex that checks if the first character of a string is `!` or not? – Nisarg Shah Aug 13 '17 at 13:32
-
1What have you tried? This is not how SO works. You show your attempt and people provide fixes. – Script47 Aug 13 '17 at 13:34
-
i know how to check the FIRST CHAR IN STRING but my question is - how can i check the (!) exclamation char ... i find only things for A-Z or number but not ONLY for that – Mr.Butterverleih Aug 13 '17 at 13:56
1 Answers
0
//using regex
let a = "!variable";
let test = /!/.test(a);
if (test) console.log("there is \"!\" somewhere in \"a\" variable");
//or not using regex
let char = a.split("", 1)[0];
if (char == "!") console.log("first letter is \"!\"");
The second part takes first letter and logs message if it is "!" , first part tests whole string in order to find "!". I'm not sure if there is a way to check that first letter is "!" using regex.

gravig
- 26
- 4