2015-05-28 56 views
-3

我在查詢wiktionary API,並且需要匹配「===名詞===」,包括之後的文本,直到「==== Translations ====」開始。實際的JSON對象比較大,我需要匹配幾次,包括「===動詞===」等。在Regexr.com上,我設法讓它匹配,但不是在我的JavaScript:正則表達式不匹配 - Javascript

var regex = /(===Verb===|===Noun===|===Adjective===|===Adverb===).*?====/g; 
    console.log(jsondata.match(regex)); 
    console.log(regex.test(jsondata)); 

任何幫助非常感謝!

==English== ===Pronunciation=== * , * , * * * * ===Noun=== # [[feces|Feces]]. ====Synonyms==== * [[BM]] * [[doo-doo]] * [[poo]] * [[poop]] ===Interjection=== # #* '''1995''', Phil Farrand, ''The Nitpicker's Guide for Next Generation Trekkers: Volume 2'' #*: (Ever feel like you've just entered... The Twilight Zone? '''Doo''', doo, doo, doo, doo, doo....) #* '''2006''', Steve Taylor, ''A to X of Alternative Music'' (page 272) #*: the bloke who sang about coloured girls going ''''doo''' de doo de doo doo d'de doo de doo de doo' had once had this thing with the guy who produced the debut albums by the Stooges and Patti Smith. ====Related terms==== * [[doo-wop]] ---- ==Gooniyandi== ===Noun=== # [[cave]] ---- ==Manx== ===Etymology=== From , from , from . ===Adjective=== # [[black]] # [[inky]] ====Synonyms==== * ====Derived terms==== * ===Noun=== # [[ink]] ====Derived terms==== ===Verb=== # to [[ink]] ===Mutation=== [[Category:gv:Colors]] ---- [[Category:Navajo terms with audio links]] ==Navajo== ===Pronunciation=== * ===Particle=== # Part of the [[negative]] correlative: #: '''''doo''' ... da'': #:: # With a nominalizer, forms a negative noun phrase: #: #: #: # Pairing '''doo''' with a verb + [[-góó]] forms a negative conditional: #: ====Derived terms==== ===Pronunciation=== * ===Verb=== # ''it will be'' (abbreviated form of [[dooleeł]]) # paired with [[ńtʼééʼ]], it forms a conditional: #: #: ====See also==== * * ---- ==Portuguese== ===Verb=== # # ---- ==Rohingya== ===Noun=== # [[knife]] ---- ==Scots== ===Etymology=== From (compare woman's [[given name]]); akin to Old High German , Icelandic , ''[[Dúfa]]'' "[[Dove]]" (woman's [[first name]]), Swedish , Danish and Norwegian . ===Pronunciation=== * ===Noun=== # [[dove]] (bird of the pigeon family, [[Columbidae]]) ====Derived terms==== * [[King of the Doos]] [[es:doo]] [[fr:doo]] [[lt:doo]] [[mg:doo]] [[pl:doo]] [[ru:doo]] [[fi:doo]] [[tr:doo]]

+0

究竟這是怎麼不工作? –

+0

你至少可以嘗試格式化一些問題,讓人們不會暈頭轉向。 – Craicerjack

+0

更好地使用一些XML解析器,請參閱http://stackoverflow.com/q/7949​​752/1682509 – Reeno

回答

0

確保你的json被正確地轉義了。

在我的例子,我躲過了話ōʹvə-flō'ō'və-flōʹōʹvər-flō'ō'vər-flōʹōʹvə-flō\'ō\'və-flōʹōʹvər-flō\'ō\'vər-flōʹ,分別工作:

var json = '<root><h level="2" i="1">==English==</h> <template lineStart="1"><title>wikipedia</title><part><name>dab</name>=<value>overflow</value></part><part><name>lang</name>=<value>en</value></part></template> <h level="3" i="2">===Etymology===</h> From <template><title>prefix</title><part><name index="1"/><value>over</value></part><part><name index="2"/><value>flow</value></part><part><name>lang</name>=<value>en</value></part></template>. Literally corresponds to <template><title>term</title><part><name index="1"/><value>superfluous</value></part><part><name>lang</name>=<value>en</value></part></template>, which is from Latin, rather than Germanic. <h level="3" i="3">===Pronunciation===</h> * <template><title>a</title><part><name index="1"/><value>RP</value></part></template> ** <template><title>sense</title><part><name index="1"/><value>noun</value></part></template> <template><title>enPR</title><part><name index="1"/><value>ōʹvə-flō\'</value></part></template>, <template><title>IPA</title><part><name index="1"/><value>/ˈəʊvəˌfləʊ/</value></part><part><name>lang</name>=<value>en</value></part></template> ** <template><title>sense</title><part><name index="1"/><value>verb</value></part></template> <template><title>enPR</title><part><name index="1"/><value>ō\'və-flōʹ</value></part></template>, <template><title>IPA</title><part><name index="1"/><value>/ˌəʊvəˈfləʊ/</value></part><part><name>lang</name>=<value>en</value></part></template> * <template><title>a</title><part><name index="1"/><value>GenAm</value></part></template> ** <template><title>sense</title><part><name index="1"/><value>noun</value></part></template> <template><title>enPR</title><part><name index="1"/><value>ōʹvər-flō\'</value></part></template>, <template><title>IPA</title><part><name index="1"/><value>/ˈoʊvɚˌfloʊ/</value></part><part><name>lang</name>=<value>en</value></part></template> ** <template><title>sense</title><part><name index="1"/><value>verb</value></part></template> <template><title>enPR</title><part><name index="1"/><value>ō\'vər-flōʹ</value></part></template>, <template><title>IPA</title><part><name index="1"/><value>/ˌoʊvɚˈfloʊ/</value></part><part><name>lang</name>=<value>en</value></part></template> * <template><title>rhymes</title><part><name index="1"/><value>əʊ</value></part><part><name>lang</name>=<value>en</value></part></template> <h level="3" i="4">===Noun===</h> <template lineStart="1"><title>en-noun</title></template> # The [[spillage]] resultant from overflow; [[excess]]. # [[outlet|Outlet]] for escape of excess material. # <template><title>context</title><part><name index="1"/><value>computing</value></part><part><name>lang</name>=<value>en</value></part></template> The situation where a value exceeds the available [[numeric]] [[range]]. <h level="4" i="5">====Translations====</h> <template lineStart="1"><title>trans-top</title><part><name index="1"/><value>spillage</value>'; 
 

 
var regex = new RegExp(/(===Verb===|===Noun===|===Adjective===|===Adverb===).*?====/g); 
 

 
document.write(json.match(regex));

+1

非常感謝,工作! – Lukars