1
我有這樣的東西維基百科文章內容:如何從字符串中刪除所有Wiki模板?
{{Use mdy dates|date=June 2014}}
{{Infobox person
| name = Richard Matthew Stallman
| image = Richard Stallman - Fête de l'Humanité 2014 - 010.jpg
| caption = Richard Stallman, 2014
| birth_date = {{Birth date and age|1953|03|16}}
| birth_place = New York City
| nationality = American
| other_names = RMS, rms
| known_for = Free software movement, GNU, Emacs, GNU Compiler Collection|GCC
| alma_mater = Harvard University,<br />Massachusetts Institute of Technology
| occupation = President of the Free Software Foundation
| website = {{URL|https://www.stallman.org/}}
| awards = MacArthur Fellowship<br />EFF Pioneer Award<br />''... see #Honors and awards|Honors and awards''
}}
或
{{Citation needed|date=May 2011}}
如何去除呢?我可以使用這個正則表達式:/\{\{[^}]+\}\}/g
,但它不會工作嵌套模板像Infobox
我試圖使用此代碼首先刪除嵌套的模板,然後刪除信息框,但我得到了錯誤的結果。
var input = document.getElementById('input');
input.innerHTML = input.innerHTML.replace(/\{\{[^}]+\}\}/g, '');
<pre id="input"> {{Use mdy dates|date=June 2014}}
{{Infobox person
| name = Richard Matthew Stallman
| image =Richard Stallman - Fête de l'Humanité 2014 - 010.jpg
| caption = Richard Stallman, 2014
| birth_date = {{Birth date and age|1953|03|16}}
| birth_place = New York City
| nationality = American
| other_names = RMS, rms
| known_for = Free software movement, GNU, Emacs, GNU Compiler Collection|GCC
| alma_mater = Harvard University,<br />Massachusetts Institute of Technology
| occupation = President of the Free Software Foundation
| website = {{URL|https://www.stallman.org/}}
| awards = MacArthur Fellowship<br />EFF Pioneer Award<br />''... see #Honors and awards|Honors and awards''
}}</pre>
@yurzui這不會對文本的工作,包含{{}}在一個以上的地方https://regex101.com/r/kG7bO0/2 – jcubic
@jcubic你的意思是'foo'不應該匹配? – tchelidze
如果你可以在兩個步驟中完成,你可以匹配內部第一個然後外部,這對於內部https://regex101.com/r/pG5sS0/1 –