Can Javascript RegExp do partial case insensitivity?

后端 未结 5 567
我寻月下人不归
我寻月下人不归 2021-01-14 14:35

I want to know if Javascript RegExp has the ability to turn on and off case insensitivity within the regular expression itself. I know you can set the modifier for the entir

5条回答
  •  伪装坚强ぢ
    2021-01-14 14:54

    Why not simply use 2 Regex's?

    var str = "teXT To seArcH TOP SECRET";
    if (/text to search/i.test(str) && /TOP SECRET/.test(str)) {
      console.log('match!');
    }
    

    I think there is rarely a good reason to make a single very complex regex to cover every case, when you could instead make a handful of Regex's which you combine with code logic. I'd much rather have small bite size Regex's from a maintenance point of view, as figuring what a complex regex actually does can be quite daunting.

提交回复
热议问题