Javascript 如何解析数组中多次出现的命令字符串?

Javascript 如何解析数组中多次出现的命令字符串?,javascript,html,Javascript,Html,我正在构建一个模板引擎,我有以下输入: <!-- #page main --> <!-- #content head --> <head></head> <!-- #content body --> Some text inside body content <body></body> <!-- #content footer --> <footer></footer> 以及

我正在构建一个模板引擎,我有以下输入:

<!-- #page main -->
<!-- #content head -->
<head></head>
<!-- #content body -->
Some text inside body content
<body></body>
<!-- #content footer -->
<footer></footer>
以及输出:

{ 1: 'info 1', 2: 'info 2\n more info 2', 3: 'info 3' }

好的,你必须在字符串中循环寻找某个关键字,在本例中是,
command
,然后继续在字符串中循环,直到找到紧跟在该关键字后面的字符,并继续查找,直到找到无效字符(在本例中为非整数),然后保存当前主字符串的索引,然后继续循环并再次查找相同的关键字,如果找到,则保存索引,然后创建一个包含从第一个索引到最后一个刚找到的索引的内容的新对象,并将其推送到某个数组或其他地方,然后继续沿着主字符串。如果没有找到附加关键字,则将最终索引视为是下一个关键字的索引等。 整个中间过程应该是它自己的函数,带有一个包含主原始字符串、起始索引、关键字和某种数组的参数,以将值推入其中(或者让它返回此值)然后,在主代码范围中,当您找到关键字etc时,开始调用该函数。树函数应该以某种方式(也)返回找到end关键字所在的索引,这可以用于递归调用它,直到索引为-1,在这种情况下,可以改为使用主字符串的结尾

这至少是我头脑风暴时的理论,但实现可能更简单

无论如何,基本上


无标题
ok.onkeyup=ok.onchange=ok.onfocus=
函数(){
ko.innerText=JSON
.stringify(解析它(ok.value))
}
函数parseIt(str){
功能
getContentBetweenKeywords(关键字,单词列表){
var contents=[]
getIt()
函数getIt(){
变量i,k
对于(i=0;i-1?单词列表[foundIndex]。索引:str.length
内容[
单词表[i+1]。word
]=(str.substring(wordList[i+1]。index+wordList[i+1]。word.length,endInd))
}
}
}
}
返回内容
}
函数getKeywords(str、start、end){
变量i,chars=“QWERTYUIOPLKJHGFDSAZXCVBNMqwertyuioplkjhgfdsazxcvbnm1234567890$”,
startWord=-1,
单词=[]
对于(i=0;i
或者您可以通过以下脚本实现相同的功能:

const text=`command 1
信息1
命令2
信息2
更多信息2
命令3
信息3`,文本2=
`
正文内容中的一些文本
`;
log(xtract(文本,“命令”));
日志(xtract(text2,#content,2));
函数xtract(文本,关键字,idx=1){
让res=text.split(“\n”).reduce((acc,line)=>{
让kw=line.trim().split(“”;//尝试关键字:
if(line.match(关键字))acc.curr=(acc[kw[idx]]=acc[kw[idx]]]| |[]);
否则,如果(根据当前)根据当前推动(线路修剪());
返回acc;
}, {}); 
删除res.curr;
//如果您不想要数组,而是想要简单的文本字符串作为属性:
Object.keys(res).forEach(k=>res[k]=res[k].join(“\n”))
返回res;

}
您是否可以使用另一个知道如何读取html/xml的库?或者你必须从头开始做这件事吗?看看getElementsByTagName(“*”);我想自己编写代码,这个命令也使用html语法,它可以是任何东西。看起来像是DomParser而不是regex的工作。dom中的注释有一个节点类型和文本内容,因此易于阅读。例如,我可以制作一个小版本的DomParser来解决这个特定问题吗?我不想要完整臃肿的DomParser代码。
const regPageContent = /<!-- #content \b(\w+)\b -->\n([\s\S]*)/;
const regPageContentRest = /([\s\S]*)<!-- #content \b\w+\b -->/;


function page(page, layout, replace) {

  if (!layout) {
    return [
      fappend(page)
    ];
  }
  let res = [];
  let rest = page;
  let match,
      secondmatch;

  if (replace) {
    secondmatch = rest.match(regPageContentRest);

    if (secondmatch) {
      res.push(
        freplace(replace, secondmatch[1])
      );
      rest.substring(secondmatch[1].length);
    } else {
      res.push(
        freplace(replace, rest)
      );
    }

  while (rest.length > 0) {

    match = rest.match(regPageContent);

    if (match) {
      secondmatch = match[2].match(regPageContentRest);

      console.log(rest);
      console.log('rest');
      console.log(match[2]);

      if (secondmatch) {
        res.push(
          freplace(match[1], secondmatch[1])
        );
        rest.substring(secondmatch[1].length);
      } else {
        res.push(
          freplace(match[1], match[2])
        );
        break;
      }
    } else {
      break;
    }
  }

  res.push(flayout(layout));

  return res;
}
command 1
  info 1
command 2
  info 2
  more info 2
command 3
info 3
{ 1: 'info 1', 2: 'info 2\n more info 2', 3: 'info 3' }