如何在下面的ES6循环中获得前面的文本
How can I get the preceding text in the following ES6 loop?
我正在用ES6:编写Markdown解析器
输入:
# Title
* * *
Paragraph
Another paragraph
示例代码:
// create a find/replace based on a regex and replacement
// to later be used with the conversion functions
function massReplace(text, replacements) {
let result = text
for (let [regex, replacement] of replacements) {
result = result.replace(regex, replacement)
}
return result
}
// match text with # and replace them for html headings
function convertHeadings(text, orig) {
if (orig.match(/^#{1,6}'s/)) {
return massReplace(text,
[/^### (.*)/gm, '<h3>$1</h3>'],
[/^## (.*)/gm, '<h2>$1</h2>'],
[/^# (.*)/gm, '<h1>$1</h1>'] ]
)
}
}
// match text without # and surround them with p tags
function convertParagraphs(text, orig) {
if (!orig.match(/^#{1,6} (.*)/)) {
return `<p>${text}</p>`
}
}
// take the source, split on new lines, make a copy (to
// have a "clean" version to be used in the if statements),
// and finally apply the conversion functions to them with
// the help of a loop and excluding those that output undefined
function convertToHTML(markdownSource) {
let data = markdownSource.split(''n'n')
, orig = data.slice()
, conversions = [ convertHeadings, convertParagraphs]
for (let i = 0, l = orig.length; i < l; ++i) {
for (let conversion of conversions) {
let result = conversion(data[i], orig[i])
if (result !== undefined) {
data[i] = result
}
}
}
return data.join(''n'n')
}
我现在想要的是用类no-indent
将p
标记包装在前面有* * *
的文本周围(上例中为Paragraph
)。问题是,我不知道如何根据前一个(本例中为* * *
)获得文本。
为了给出一个想法,这是所需的输出:
<h1>Title</h1>
<p>* * *</p>
<p class="no-indent">Paragraph</p>
<p>Another paragraph</p>
您正在询问一个关于标记化和解析的问题,特别是可能的前瞻性解析:
维基百科页面:
https://en.wikipedia.org/wiki/Lexical_analysis
https://en.wikipedia.org/wiki/Parsing
StackOverflow标记化:
https://stackoverflow.com/questions/tagged/token
https://stackoverflow.com/questions/tagged/tokenize
StackOverflow解析问题:
https://stackoverflow.com/questions/tagged/parsing
相关文章:
- 如何使用jquery在填充自动完成的值后使文本框只读
- 使用Clipboard.js复制span文本
- 如何在下面的ES6循环中获得前面的文本
- 当
不在 前面时查找文本 - 在硒的文本前面增加变量的值
- 如何在页面上选择文本块,然后将元素移动到其前面
- 将表单文本框和按钮放在每个单选按钮的前面
- 使用 jquery 在每个文本区域行的前面添加“>”符号
- 在文本框中的值前面添加零
- 如何替换</身体>并在其前面插入文本
- 在文本框中输入文本时,如果字母表前面有空格,请将字母表输入大写
- 在JavaScript中用前面的标签填充文本框
- D3.js:文本不显示在圆圈内(它们在元素组的同一层).我怎样把文字放到前面
- 检查文本'ABC'它的前面和后面应该只有一个数字,后面跟着一个空格
- 只更改背景图像平滑,前面没有文本
- 在页面上的所有文本框前面添加一个$符号
- 如何将光标移动到文本框的前面,其中包含文本
- 将span标记添加到前面有特定字符的文本中
- 如何在CSS浏览器中使日语字符(长文本)前面的数字出现在一行中
- 如何在Mozilla的自定义文本框中移动光标到插入的文本前面