1

さて、ちょっとした練習として 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部分の作成には興味がありません。これは私の質問ではなく、問題でもありません。質問は正規表現です。

4

2 に答える 2

0

だから私はこれに長く懸命に取り組んできましたが、実際にはうまくいくとは思えないので、代わりにjsonファイルをロードして使用しました

JSON.parse()

私が望んでいたことをします。

于 2013-03-27T09:59:46.383 に答える
-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];
}

しかし、それは完璧ではありません。

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

于 2013-03-19T14:39:44.363 に答える