1

Okay, I'm playing with some JavaScript regular expressions as a bit of practice and I've set myself the task of turning this text file into a json object:

Text file:

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. 

into this:

{
    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. '
}

I figured regular expressions would be best for this (since it's a Node experiment) and currently I have this expression (which I'm sure can be greatly improved) but it has an issue with the body containing line breaks between paragraphs.

/^(.*)\: ?\n?(.*)/gim

How can I get it to select the whole body text instead of skipping the middle paragraph?

[edit]

To clarify, I'm not interested in the making json part, this isn't my question and not a problem, it's the regular expression that is the question.

4

2 回答 2

0

So I worked long and hard on this, doesn't appear it can actually be done nicely so I've gone with loading a json file instead and using

JSON.parse()

Does what I wanted it to.

于 2013-03-27T09:59:46.383 回答
-1

The only way that I found to do it is:

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];
}

But it isn't perfect.

example: http://jsfiddle.net/23UnR/

于 2013-03-19T14:39:44.363 回答