admin管理员组文章数量:1435859
I have a long string with many different brackets/braces/parenthesis ({{...}},{...},[...], [[...]], <...>) with some being inside others.
I would like to use JS to remove these brackets and whatever text/etc is inside them (except for [[...]] that are not inside other brackets, in this case I would like only the brackets removed).
Here is an example of the type of string I'm working with:
{{pp-move-indef}} {{Taxobox | name = Red panda | status = VU | status_system = iucn3.1 | status_ref = | image = RedPandaFullBody.JPG | regnum = [[Animal]]ia | phylum = [[Chordate|Chordata]] | classis = [[Mammal]]ia | ordo = [[Carnivora]] | subordo = [[Caniformia]] | infraordo = [[Arctoidea]] | superfamilia = [[Musteloidea]] | familia = [[Ailuridae]] | genus = Ailurus | genus_authority = [[Frederic Cuvier|F. Cuvier]], 1825 | species = A. fulgens | binomial = Ailurus fulgens | binomial_authority = [[Frederic Cuvier|F. Cuvier]], 1825 | range_map = Cypron-Range Ailurus fulgens.svg | range_map_caption = Red panda range | subdivision_ranks = Subspecies | subdivision = A. f. fulgens
A. f. styani }}The red panda (Ailurus fulgens, lit. "shining cat"), is a small [[arboreal]] [[mammal]] native to the eastern [[Himalayas]] and southwestern [[China]]{{IUCN |assessors=Wang, X., Choudhry, A., Yonzon, P., Wozencraft, C., Than Z. |year=2008 |id=714 |taxon=Ailurus fulgens |version=2010.4 |downloaded=26 June 2010}} and related to [[raccoon]]s, [[skunk]]s and [[weasel]]s. It is the only extant species of the genus Ailurus and the family [[Ailuridae]]. Slightly larger than a [[domestic cat]], it has reddish-brown fur, a long, shaggy tail, and a waddling gait due to its shorter front legs. It feeds mainly on [[Bamboo shoot|bamboo]], but is [[omnivorous]] and may also eat eggs, [[bird]]s, [[insect]]s, and small [[mammal]]s. It is a [[solitary animal]], mainly [[Nocturnality|active from dusk to dawn]], and is largely [[Sedentary lifestyle|sedentary]] during the day.
Desired Result
The red panda (Ailurus fulgens, lit. "shining cat"), is a small arboreal mammal native to the eastern Himalayas and southwestern China and related to raccoons, skunks and weasels. It is the only extant species of the genus Ailurus and the family Ailuridae. Slightly larger than a domestic cat, it has reddish-brown fur, a long, shaggy tail, and a waddling gait due to its shorter front legs. It feeds mainly on Bamboo shoot|bamboo, but is omnivorous and may also eat eggs, birds, insects, and small mammals. It is a solitary animal, mainly Nocturnality|active from dusk to dawn, and is largely Sedentary lifestyle|sedentary during the day.
I have a long string with many different brackets/braces/parenthesis ({{...}},{...},[...], [[...]], <...>) with some being inside others.
I would like to use JS to remove these brackets and whatever text/etc is inside them (except for [[...]] that are not inside other brackets, in this case I would like only the brackets removed).
Here is an example of the type of string I'm working with:
{{pp-move-indef}} {{Taxobox | name = Red panda | status = VU | status_system = iucn3.1 | status_ref = | image = RedPandaFullBody.JPG | regnum = [[Animal]]ia | phylum = [[Chordate|Chordata]] | classis = [[Mammal]]ia | ordo = [[Carnivora]] | subordo = [[Caniformia]] | infraordo = [[Arctoidea]] | superfamilia = [[Musteloidea]] | familia = [[Ailuridae]] | genus = Ailurus | genus_authority = [[Frederic Cuvier|F. Cuvier]], 1825 | species = A. fulgens | binomial = Ailurus fulgens | binomial_authority = [[Frederic Cuvier|F. Cuvier]], 1825 | range_map = Cypron-Range Ailurus fulgens.svg | range_map_caption = Red panda range | subdivision_ranks = Subspecies | subdivision = A. f. fulgens
A. f. styani }}The red panda (Ailurus fulgens, lit. "shining cat"), is a small [[arboreal]] [[mammal]] native to the eastern [[Himalayas]] and southwestern [[China]]{{IUCN |assessors=Wang, X., Choudhry, A., Yonzon, P., Wozencraft, C., Than Z. |year=2008 |id=714 |taxon=Ailurus fulgens |version=2010.4 |downloaded=26 June 2010}} and related to [[raccoon]]s, [[skunk]]s and [[weasel]]s. It is the only extant species of the genus Ailurus and the family [[Ailuridae]]. Slightly larger than a [[domestic cat]], it has reddish-brown fur, a long, shaggy tail, and a waddling gait due to its shorter front legs. It feeds mainly on [[Bamboo shoot|bamboo]], but is [[omnivorous]] and may also eat eggs, [[bird]]s, [[insect]]s, and small [[mammal]]s. It is a [[solitary animal]], mainly [[Nocturnality|active from dusk to dawn]], and is largely [[Sedentary lifestyle|sedentary]] during the day.
Desired Result
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Mar 24, 2013 at 2:26 user989990user989990 1751 gold badge7 silver badges15 bronze badges 4The red panda (Ailurus fulgens, lit. "shining cat"), is a small arboreal mammal native to the eastern Himalayas and southwestern China and related to raccoons, skunks and weasels. It is the only extant species of the genus Ailurus and the family Ailuridae. Slightly larger than a domestic cat, it has reddish-brown fur, a long, shaggy tail, and a waddling gait due to its shorter front legs. It feeds mainly on Bamboo shoot|bamboo, but is omnivorous and may also eat eggs, birds, insects, and small mammals. It is a solitary animal, mainly Nocturnality|active from dusk to dawn, and is largely Sedentary lifestyle|sedentary during the day.
- Please post the approaches you have tried so far. What did not work and where exactly are you stuck? – Felix Kling Commented Mar 24, 2013 at 2:27
-
Tried
string.replace(some regex,'')
but don't know enough to get the desired results. Can this be done with.replace()
? – user989990 Commented Mar 24, 2013 at 2:32 -
Ah, so the "nested brackets" will always be consecutive? I.e. it will always be
{{...}}
but never{...{...}...}
? – Felix Kling Commented Mar 24, 2013 at 2:43 - I believe that is correct. – user989990 Commented Mar 24, 2013 at 2:48
2 Answers
Reset to default 3function removeBrackets(input) {
return input
.replace(/{.*?}/g, "")
.replace(/\[.*?\]/g, "")
.replace(/<.*?>/g, "")
.replace(/\(.*?\)/g, "");
}
Note: this builds on @Joseph Lennox's suggestion above.
function removeBrackets(input) {
return input
.replace(/{+.*?}+/g, "")
.replace(/\[\[|\]\]/g, "")
.replace(/<.*?>/g, "");
}
... the + let's us absorb {{ and }}, etc.
本文标签: regexRemove Brackets and Text Between Using JavascriptStack Overflow
版权声明:本文标题:regex - Remove Brackets and Text Between Using Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745675343a2669818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论