两个字符串之间的Nodejs readFileSync正则表达式标记

Nodejs readFileSync regex tag between two strings

我正在尝试借助 nodejs fs.readFileSync() 函数将文件内容提取到字符串中,如下所示:

let fileData = fs.readFileSync('./path, 'utf8');

之后我想通过regex:

获取两个字符串之间的内容

我有一个像这样的字符串:

<route-meta>
    {
        "requiresAuth": true
    }
</route-meta>

<template>

</template>

<script>
    export default {
        name: "check"
    }
</script>

<style scoped>

</style>

我需要在 <route-meta></route-meta> 之间找到 text/string,这样结果应该是:

{
   "requiresAuth": true
}

我尝试用 fileData.match(/<route-meta>(.*?)<\/route-meta>/); 这样做,但不知何故它给了我 null 结果

我遵循了这个例子:Regex match text between tags

我也尝试检查 fileData.replace('route-meta', ''); 但它仍然没有给我想要的结果我的意思是我无法使用 fileData.

进行任何字符串操作

指导我哪里做错了。

请不要使用正则表达式进行 XML 解析

但是要回答你的问题,你可以试试这个

let re = /<route-meta>[\s\S]*?<\/route-meta>/
let re1 = /<(\/*)route-meta>/gi
let newstr = fileData.match(re)[0].replace(re1,"")
console.log(newstr)

re 使用 match
找到内容完整的标签 re1 使用 replace

删除开始和结束标签