我正在使用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>
```
['新正則表達式(matches.map(RegExp.escape)。加入( '\\ S *'))'](https://github.com/benjamingr/RegExp.escape/blob/master /polyfill.js)你可能也有興趣[**混合組合**](http://stackoverflow.com/questions/5752002/find-all-possible-subset-combos-in-an-array) –
@PaulS。 'matches.map'看起來很有希望,你能否添加一個例子作爲答案,以便我可以標記它? –