我将.txt格式的旧维基文本转换为Markdown(.md)
我在txt文本中有一些javascript示例,并希望正确包装它们,因此我使用:/(<code javascript\>([\s\S]*)<\/code\>)/
来匹配这些部分。
这个正则表达式仍然遗漏了一些部分。以下是问题的一个示例:
var text = 'This is a text to test:<code javascript>var x = {foo: bar}; alert(JSON.stringify(x));</code>Then any string would inherit that function:<code javascript>"hey howdy!".alert();</code>End of test';
text = text.replace(/(<code javascript\>([\s\S]*)<\/code\>)/, function(string, submatch1, submatch2){
console.log(submatch2);
return '```js' + submatch2 + '```';
});
此日志仍在其中标记</code>
。
var x = {foo: bar}; alert(JSON.stringify(x));</code>Then any string would inherit that function:<code javascript>"hey howdy!".alert();
我错过了什么?
答案 0 :(得分:1)
你正在做一个贪婪的搜索,它将匹配下一个符号的最后一次出现。您只需在?
之后加*
即可进行懒惰搜索,这意味着它会在看到<\/code\>
/(<code javascript\>([\s\S]*?)<\/code\>)/
更新:匹配所有出现次数
/(<code javascript\>([\s\S]*?)<\/code\>)/g
答案 1 :(得分:1)
必须解决两个错误:
1)通过添加?
2)通过添加g
/(<code javascript\>([\s\S]*?)<\/code\>)/g