jquery check if string starts with 1234

后端 未结 1 1107
無奈伤痛
無奈伤痛 2020-12-30 00:59

Thanks for taking the time to answer my question.

I want to check if a string has exactly 7 characters and starts with \"1234\". How do I do that?

I know of

相关标签:
1条回答
  • 2020-12-30 01:19

    Simple RegExp:

    var isMatch = /^1234...$/.test(myString);
    

    Or:

    var isMatch = myString.length == 7 && myString.indexOf("1234") == 0;
    

    Edit: Or:

    var isMatch = myString.length == 7 && myString.substr(0, 4) == "1234";
    
    0 讨论(0)
提交回复
热议问题