Search code examples
javascriptregexletter

javascript (iQuery ) first letter is exclamation


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


Solution

  • //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.