且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java匹配与JavaScript匹配之间的结果差异

更新时间:2022-05-12 10:26:34

在JavaScript match 中返回与使用的正则表达式匹配的子字符串.在Java中, matches 检查整个字符串是否与正则表达式匹配.

In JavaScript match returns substrings which matches used regex. In Java matches checks if entire string matches regex.

如果要查找与正则表达式匹配的子字符串,请使用Pattern和Matcher类,如

If you want to find substrings that match regex use Pattern and Matcher classes like

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(yourData);
while(m.find()){
   m.group();//this will return current match in each iteration
   //you can also use other groups here using their indexes
   m.group(2);
   //or names (?<groupName>...)
   m.group("groupName");
}