js/java 解析{{}}大括号里面的变量

1.js

let content = "456{
         
  {test}}123{
         
  {chart}}321";
let regex3 = /{
         
  {(.+?)}}/g;  // {
         
  {}} 匹配大括号
let vars = content.match(regex3);
if(vars){
    for(let v of vars){
        console.log(v);//{
         
  {test}},{
         
  {chart}}
    }
}

2.java

String content = "456{
         
  {test}}123{
         
  {chart}}321"
    String reg = "\{\{(.+?)\}\}";
    String[] vars = GetRegResult(reg, content);//将content解析之后的数组
    System.out.print(vars);//[{
         
  {test}},{
         
  {chart}}]


    /**
	 * 该函数功能是正则匹配出所有结果到一个String数组中
	 */
	public static String[] GetRegResult(String pattern, String Sourcecode) {
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(Sourcecode);

		ArrayList<String> tempList = new ArrayList<String>();
		while (m.find()) {
			tempList.add(m.group());
		}
		String[] res = new String[tempList.size()];
		int i = 0;
		for (String temp : tempList) {
			res[i] = temp;
			i++;
		}
		return res;
	}
经验分享 程序员 微信小程序 职场和发展