Javascript YAML 前端问题的正则表达式
Javascript Regex for YAML Front End Matter
我正在使用框架将 Markdown 呈现为 HTML - 但它不考虑 YAML 前端问题。它只是将其呈现为两个 <hr>
,然后将其之间的文本呈现为 <p>
.
中的字符串
我想为框架创建一个插件,能够呈现任何前端问题(如果存在)。
我已经尝试研究,但似乎找不到任何关于如何匹配所需的两个 ---
的信息。
我尝试创建自己的正则表达式:https://regex101.com/r/ZmKyvP/2 但我似乎无法获得 ---
之间的信息。我似乎只匹配那些项目。
有没有简单的方法可以将其转换为 JSON?
降价
---
title: My awesome blog
date: 2020-06-30
tags: dogs,doggo,pupper,floofer,woofters
description: tl;dr
---
Don't capture me!
正则表达式
// current regex
/^(---)(.*)(---)$/g
希望输出
{
"title": "My awesome blog",
"date": "2020-06-30",
"tags": ["dogs","doggo","pupper","floofer","woofters"],
"description": "tl;dr"
}
既然您已经在使用 javascript 为什么不使用一些 javaScript 字符串操作来隔离您希望使用的字符串?
function stringModification(){
const tango = "---";
var yourText = "---\ntitle: My awesome blog\ndate: 2020-06-30\n"+
"tags: dogs,doggo,pupper,floofer,woofters\ndescription: Whether you call them dogs\n---\nddddd"
var startPosition = yourText.search(tango)+tango.length;
var endPosition = yourText.slice(startPosition).search(tango)+startPosition;
yourText = yourText.slice(startPosition,endPosition);
Logger.log(yourText);
//at this point the variable yourText is isolated with proper captures.
// if you wanted to go further without Regex, you could make additional modifications...
yourText = "{" + yourText.replace(/: /g,':') +"}";
yourText = yourText.replace(/\n/g,'"\n"');
yourText = yourText.replace(/:/g,'" : "');
Logger.log(yourText);
}
我正在使用框架将 Markdown 呈现为 HTML - 但它不考虑 YAML 前端问题。它只是将其呈现为两个 <hr>
,然后将其之间的文本呈现为 <p>
.
我想为框架创建一个插件,能够呈现任何前端问题(如果存在)。
我已经尝试研究,但似乎找不到任何关于如何匹配所需的两个 ---
的信息。
我尝试创建自己的正则表达式:https://regex101.com/r/ZmKyvP/2 但我似乎无法获得 ---
之间的信息。我似乎只匹配那些项目。
有没有简单的方法可以将其转换为 JSON?
降价
---
title: My awesome blog
date: 2020-06-30
tags: dogs,doggo,pupper,floofer,woofters
description: tl;dr
---
Don't capture me!
正则表达式
// current regex
/^(---)(.*)(---)$/g
希望输出
{
"title": "My awesome blog",
"date": "2020-06-30",
"tags": ["dogs","doggo","pupper","floofer","woofters"],
"description": "tl;dr"
}
既然您已经在使用 javascript 为什么不使用一些 javaScript 字符串操作来隔离您希望使用的字符串?
function stringModification(){
const tango = "---";
var yourText = "---\ntitle: My awesome blog\ndate: 2020-06-30\n"+
"tags: dogs,doggo,pupper,floofer,woofters\ndescription: Whether you call them dogs\n---\nddddd"
var startPosition = yourText.search(tango)+tango.length;
var endPosition = yourText.slice(startPosition).search(tango)+startPosition;
yourText = yourText.slice(startPosition,endPosition);
Logger.log(yourText);
//at this point the variable yourText is isolated with proper captures.
// if you wanted to go further without Regex, you could make additional modifications...
yourText = "{" + yourText.replace(/: /g,':') +"}";
yourText = yourText.replace(/\n/g,'"\n"');
yourText = yourText.replace(/:/g,'" : "');
Logger.log(yourText);
}