Useful references:
Regular expression:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions
org.apache.regexp.RE:
https://www.cs.cmu.edu/~rcm/websphinx/doc/org/apache/regexp/RE.html
how to ignore case sensitivity examples for org.apache.regexp.RE
http://www.javadocexamples.com/org/apache/regexp/RE/RE(String%20pattern,int%20matchFlags).html
今天遇到一个正则表达式不区分大小写的问题,现将学习结果总结下
1. 普通的regex
使用一个正则表达式字面量,其由包含在斜杠之间的模式组成,如下所示:
const regex = /ab+c/;
const regex = /^[a-zA-Z]+[0-9]*\W?_$/gi; // g全局搜索 i不区分大小写搜索
或者调用RegExp
对象的构造函数,如下所示:
let regex = new RegExp("ab+c");
let regex = new RegExp(/^[a-zA-Z]+[0-9]*\W?_$/, "gi");
let regex = new RegExp("^[a-zA-Z]+[0-9]*\\W?_$", "gi");
2. org.apache.regexp.RE 不区分大小写
String pattern = "http://\\S+\\.\\S+|https://\\S+\\.\\S+"; String input = "Http://baidu.com"; // MATCH_CASEINDEPENDENT: Flag to indicate that matching should be case-independent. 也就是不区分大小写 // MATCH_NORMAL: Specifies normal, case-sensitive matching behaviour. 也就是区分大小写 RE re = new org.apache.regexp.RE(pattern, RE.MATCH_CASEINDEPENDENT); boolean accept = re.match(input); // return true