Regex to check whether string starts with, ignoring case differences

后端 未结 5 2054
野性不改
野性不改 2020-12-29 03:23

I need to check whether a word starts with a particular substring ignoring the case differences. I have been doing this check using the following regex search pattern but th

5条回答
  •  再見小時候
    2020-12-29 03:44

    I think all the previous answers are correct. Here is another example similar to SERPRO's, but the difference is that there is no new constructor:

    Notice: i ignores the case and ^ means "starts with".

     var whateverString = "My test String";
     var pattern = /^my/i;
     var result = pattern.test(whateverString);
     if (result === true) {
         console.log(pattern, "pattern matched!");
     } else {
         console.log(pattern, "pattern did NOT match!");
     }
    

    Here is the jsfiddle (old version) if you would like to give it a try.

提交回复
热议问题