0
我想根據標題按照特定格式(==HEADER==
)將字符串分割爲多個部分。這裏的輸入字符串會是什麼樣子:根據標題將字符串分割爲多個部分
== Section header ==
Text inside section
=== Maybe a nested section ===
With some more text
And more text
==Then the next section header, perhaps w/o spaces between text and equals signs==
With text inside it
這是我想要的輸出:
[
'== Section header ==
Text inside section
=== Maybe a nested section ===
With some more text
And more text',
'==Then the next section header, without spaces between text and equals signs==
With text inside it'
]
我試圖做
pagetext = "== Test header ==\n Some test text, with random equals signs==newlines\n or whatever\n ==Another header == \n more text,\nnewlines\nohmy"
sections = [];
section_re = /==\s*(\s*[^=]*)\s*==/g;
var section_headers = pagetext.match(section_re);
for (var i = 0; i < section_headers.length; i++) {
var section_start = pagetext.indexOf(section_headers[i]);
var section_text = pagetext.substring(section_start);
if (i < section_headers.length - 1) {
var section_end = section_text.substring(section_headers[i].length).indexOf(section_headers[i + 1]) + section_headers[i].length;
section_text = section_text.substring(0, section_end);
}
sections.push(section_text);
}
但它分割在「隨機等號」跡象,給我:
["== Test header ==\n Some...ith random equals signs", "==newlines\n or whatever...ore text,\nnewlines\nohmy"]
這不是 對。我有一種感覺,我的代碼可能太複雜 - 有沒有更好的方法來做到這一點?
哇一展身手,看起來超讚!快速的問題:它會突破部分頭像'==你好= Goodbye =='(它仍然是合法的頭)? – tehsockz
ok試試 'result = subject.match(/ \??\ n?)(?!== [^ =])。*)*/img);' – Brendan