regex - Remove Brackets and Text Between Using Javascript - Stack Overflow

I have a long string with many different bracketsbracesparenthesis ({{...}},{...},[...], [[...]], <

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

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.

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 4
  • 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
Add a ment  | 

2 Answers 2

Reset to default 3
function 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745675344a4639653.html

相关推荐

  • regex - Remove Brackets and Text Between Using Javascript - Stack Overflow

    I have a long string with many different bracketsbracesparenthesis ({{...}},{...},[...], [[...]], <

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信