Wildcard string comparison in Javascript

后端 未结 7 2176
花落未央
花落未央 2020-12-01 06:00

Let\'s say I have an array with many Strings Called \"birdBlue\", \"birdRed\" and some other animals like \"pig1\", \"pig2\"

7条回答
  •  一个人的身影
    2020-12-01 06:43

    You should use RegExp (they are awesome) an easy solution is:

    if( /^bird/.test(animals[i]) ){
        // a bird :D
    }
    

提交回复
热议问题