1. 程式人生 > >使用Java或 JavaScript獲取 方括號中的內容

使用Java或 JavaScript獲取 方括號中的內容

學習 return 沒有 mpi pil indexof javascrip 進行 con

1.使用Java獲取方括號中的內容

 1 String str = "[你]們,[我]們,[他]們,都要[好好學習,天天敲代碼]";
 2 Pattern p = Pattern.compile("\\[(.*)\\]");
 3 Matcher matcher = p.matcher(str);
 4 while(matcher.find()){
 5      System.out.println(matcher.group(0));
 6      //0是帶方括號,1是不帶方括號輸出內容  
 7      //list.add(matcher.group(0));  
 8      //也可以將獲取的內容放進集合裏邊進行操作
9 } 10 //判斷list中是否含有[我] 11 list.contains("[我]");

2.使用js獲取方括號中的內容

var str = ‘[你],[nihao],[wohao],[dajiahao]‘
var reg = /\[(.*)\]/gi;
var tmp  = str.match(reg);
if(tmp){
     for(var i = 0;i<tmp.length;i++){
            var s = tmp[i];
              if(str.indexOf(s)==-1){
                    return EU.msg.tip(‘沒有【中的字段】‘);
                   }
            }  
}

使用Java或 JavaScript獲取 方括號中的內容