繁体   English   中英

如何将字符串转换为数组,其中字符串为数组形式,字符串中的值在 latex

[英]How to convert a string to array, where string is in array form, the values inside string are in latex

如何将字符串转换为数组,其中字符串为数组形式,字符串内部的值在latex中,数组字符串可以嵌套任意次数。

"[[\\frac{234}{3243}],[34342]]" to [["\\frac{234}{3243}"],["34342"]]

数组字符串可以嵌套任意次数。

我不能使用 eval 和 JSON.parse,latex 不允许我这样做

const alphabetRegex = /[a-zA-Z]+/g;

const parseQuestionResponse = (userInput) => {
    let inputString = userInput.replace(/\\\\/g, "\\");
    return inputString
      .slice(1, -1)
      .split("],")
      .map(elem => {
            if (elem.length > 1) {
                elem = elem.slice(1, -1);
                userInput.match(alphabetRegex) == null ?
                    elem = elem.split(",") :
                    elem = [elem];
            }
            else elem = [""];
            return elem;
      });
  };

我尝试了一些它不起作用的情况。

使用正则表达式拆分和积极的 Lookbehind

https://regex101.com/r/biX5JV/1

 const alphabetRegex = /[a-zA-Z]+/g; const parseQuestionResponse = (userInput) => { return userInput.slice(1, -1).split(/(?<=]),/).map(elem => { if (elem.length > 1) { elem = elem.slice(1, -1); userInput.match(alphabetRegex) == null? elem = elem.split(","): elem = [elem]; } else elem = [""]; return elem; }); }; console.log(parseQuestionResponse('[[\\frac{234}{3243}],[34342]]')) console.log(parseQuestionResponse('[[\\frac{23}{\\frac{23}{23}}],[23243]]'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM