【发布时间】:2016-08-04 22:43:36
【问题描述】:
我正在使用 JavaScript/TypeScript 开发 nodeJS 应用程序。我正在用正则表达式搜索一段文本,所以我有一个匹配数组。我想要一种巧妙的方法来识别这些字符串是否相邻,除了一些可能的空格。
所以我目前的申请是这样的。我正在渲染降价,如果有两个代码块,一个紧接着另一个,我想将它们渲染为选项卡式代码块。
for (let codeBlock of codeBlocks) {
var title = /```\s?(.*?\n)/.exec(codeBlock);
var code = /```.*([\s\S]*?)```/g.exec(codeBlock)[1];
//console.log('Code: ' + code);
//console.log('Title: ' + title[1]);
result.push(code, title[1]);
var startPos = content.indexOf(code);
var containsSomething = new RegExp('/[a-z]+/i');
//if the string between the end of the last code block and the start of this one contains any content
if (containsSomething.test(content.substring(startPos, lastEndPos))) {
result.push('n'); // Not a tabbed codeblock
} else {
result.push('y')); //Is a tabbed codeblock
}
lastEndPos = code.length + startPos + title[1].length + 6;
results.push(result);
result = [];
}
因此,在下面的示例输入中,我需要区分应该标签的前两个代码块和不应该标签的第三个代码块。
``` JavaScript //in the code example above, this would be the title
var something = new somethingelse(); //in the code example above, this would be the code
```
``` CSS
.view {
display: true;
}
```
Some non-code text...
``` html
<div></div>
```
【问题讨论】:
-
@PaulS。
matches.map看起来很有希望,您能否添加一个示例作为答案,以便我标记它?
标签: javascript node.js string typescript