wiki txt用正则表达式转换为markdown

时间:2014-09-25 07:43:51

标签: javascript regex

我将.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(); 

我错过了什么?

jsFiddle:http://jsfiddle.net/dubLs4ek/

2 个答案:

答案 0 :(得分:1)

你正在做一个贪婪的搜索,它将匹配下一个符号的最后一次出现。您只需在?之后加*即可进行懒惰搜索,这意味着它会在看到<\/code\>

时停止

/(<code javascript\>([\s\S]*?)<\/code\>)/

更新:匹配所有出现次数

/(<code javascript\>([\s\S]*?)<\/code\>)/g

示例:http://regex101.com/r/sT9vD1/3

答案 1 :(得分:1)

必须解决两个错误:

1)通过添加?

来避免贪婪搜索

2)通过添加g

替换所有发生的事件
/(<code javascript\>([\s\S]*?)<\/code\>)/g

见工作小提琴:http://jsfiddle.net/dubLs4ek/1/