好的,我正在玩一些JavaScript正则表达式作为一些练习,我自己设置了将这个文本文件转换为json对象的任务:
文字文件:
title: #HEY
image: http://localhost:4089/assets/images/backgrounds/bg.jpg
date: 2013-03-19 13:48:30
excerpt: The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men.
body:
The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother\'s keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.
Normally, both your ***** would be dead as ****** fried chicken, but you happen to pull this **** while I'm in a transitional period so I don\'t wanna kill you, I wanna help you. But I can\'t give you this case, it don\'t belong to me. Besides, I\'ve already been through too much **** this morning over this case to hand it over to your dumb ***.
The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother\'s keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.
进入这个:
{
title: '#HEY',
image: 'http://localhost:4089/assets/images/backgrounds/bg.jpg',
date: '2013-03-19 13:48:30',
excerpt: 'The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother\'s keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.
Normally, both your ***** would be dead as ****** fried chicken, but you happen to pull this **** while I'm in a transitional period so I don\'t wanna kill you, I wanna help you. But I can\'t give you this case, it don\'t belong to me. Besides, I\'ve already been through too much **** this morning over this case to hand it over to your dumb ***.
The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother\'s keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee. '
}
我认为正则表达式最适合这个(因为它是一个Node实验)并且目前我有这个表达式(我肯定可以大大改进)但是它在包含段落之间的换行符的主体方面存在问题。
/^(.*)\: ?\n?(.*)/gim
如何让它选择整个正文而不是跳过中段?
[编辑]
为了澄清,我对制作json部分不感兴趣,这不是我的问题而不是问题,这是正则表达式的问题。
答案 0 :(得分:0)
所以我在这方面做了很长时间的努力,看起来它实际上并没有很好地完成,所以我选择加载一个json文件并使用
JSON.parse()
我想要它。
答案 1 :(得分:-1)
我发现这样做的唯一方法是:
var matches = content.match(/^(\w+)\: ?\n?(.*)\n/gim);
var json = {};
for(var line in matches) {
line = matches[line];
var match = line.match(/^(\w+)\: ?\n?(.*)/im);
json[match[1]] = match[2];
}
但它并不完美。