0

So I am trying to remove all parentheses and the content of them on this wikipedia page. I try using .replace(/(.*)/g, ''). The result of that is an empty string. So I tried not using the g part. I then lost a big part of the start of the text. I also tried replacing the parentheses with a word, for example Red, the result of that was:

RedRed RedRed RedRed RedRed RedRed RedRed

I was wondering if any of you knew what is happening here and if you have a solution.

Here is the text:

Norway ( ( listen); Norwegian: Norge (Bokmål) or Noreg (Nynorsk); Northern >Sami: Norga), officially the Kingdom of Norway, is a sovereign state and >unitary monarchy whose territory comprises the western portion of the Scandinavian Peninsula plus the remote island of Jan Mayen and the archipelago of Svalbard. The Antarctic Peter I Island and the sub-Antarctic Bouvet Island are dependent territories and thus not considered part of the Kingdom. Norway also lays claim to a section of Antarctica known as Queen Maud Land. Until 1814, the kingdom included the Faroe Islands, Greenland, and > Iceland. It also included Bohuslän until 1658, Jämtland and Härjedalen until > 1645, Shetland and Orkney until 1468, and the Hebrides and Isle of Man until > 1266. Norway has a total area of 385,252 square kilometres (148,747 sq mi) and a population of 5,258,317 (as of January 2017). The country shares a long > eastern border with Sweden (1,619 km or 1,006 mi long). Norway is bordered by > Finland and Russia to the north-east, and the Skagerrak strait to the south, > with Denmark on the other side. Norway has an extensive coastline, facing the > North Atlantic Ocean and the Barents Sea. King Harald V of the Dano-German House of Glücksburg is the current King of Norway. Erna Solberg became Prime > Minister in 2013, and was reelected in September, 2017. Erna Solberg replaced > Jens Stoltenberg who was the Prime Minister between 2000-2001 and 2005-2013. > A constitutional monarchy, Norway divides state power between the Parliament, > the Cabinet and the Supreme Court, as determined by the 1814 Constitution. The kingdom was established as a merger of a large number of petty kingdoms. > By the traditional count from the year 872, the kingdom has existed continuously for 1,145 years, and the list of Norwegian monarchs includes over sixty kings and earls. Norway has both administrative and political subdivisions on two levels: counties and municipalities. The Sámi people have > a certain amount of self-determination and influence over traditional territories through the Sámi Parliament and the Finnmark Act. Norway maintains close ties with both the European Union and the United States. Norway is a founding member of the United Nations, NATO, the European Free Trade Association, the Council of Europe, the Antarctic Treaty, and the Nordic Council; a member of the European Economic Area, the WTO, and the OECD; and a part of the Schengen Area. Norway maintains Nordic welfare model with universal health care and a comprehensive social security system, and Norwegian Society's values are rooted in egalitarian ideals. Defined as a The > XXI century socialism, , the Norwegian state owns key industrial sectors such > as oil (Statoil) or hydropower (Statkraft), having extensive reserves of petroleum, natural gas, minerals, lumber, seafood and fresh water. The petroleum industry accounts for around a quarter of the country's gross domestic product (GDP).

messerbill
  • 4,582
  • 1
  • 19
  • 33
Herman Neple
  • 118
  • 9

3 Answers3

2

You have to remove the parenthesis from the inside to the outside, by matching parenthesis pairs which don't contain any other parenthesis and then repeat the process until there are no pairs anymore:

function deParenthesise(text) {
  var replaced = text;
  var before = replaced;
  do {
    before = replaced;
    replaced = replaced.replace(/\([^()]*\)/g, '');
  } while (replaced != before);

  return replaced;
}

var text = "Norway ( ( listen); Norwegian: Norge (Bokmål) or Noreg (Nynorsk); Northern >Sami: Norga), officially the Kingdom of Norway, is a sovereign state and >unitary monarchy whose territory comprises the western portion of the Scandinavian Peninsula plus the remote island of Jan Mayen and the archipelago of Svalbard. The Antarctic Peter I Island and the sub-Antarctic Bouvet Island are dependent territories and thus not considered part of the Kingdom. Norway also lays claim to a section of Antarctica known as Queen Maud Land. Until 1814, the kingdom included the Faroe Islands, Greenland, and > Iceland. It also included Bohuslän until 1658, Jämtland and Härjedalen until > 1645, Shetland and Orkney until 1468, and the Hebrides and Isle of Man until > 1266. Norway has a total area of 385,252 square kilometres (148,747 sq mi) and a population of 5,258,317 (as of January 2017). The country shares a long > eastern border with Sweden (1,619 km or 1,006 mi long). Norway is bordered by > Finland and Russia to the north-east, and the Skagerrak strait to the south, > with Denmark on the other side. Norway has an extensive coastline, facing the > North Atlantic Ocean and the Barents Sea. King Harald V of the Dano-German House of Glücksburg is the current King of Norway. Erna Solberg became Prime > Minister in 2013, and was reelected in September, 2017. Erna Solberg replaced > Jens Stoltenberg who was the Prime Minister between 2000-2001 and 2005-2013. > A constitutional monarchy, Norway divides state power between the Parliament, > the Cabinet and the Supreme Court, as determined by the 1814 Constitution. The kingdom was established as a merger of a large number of petty kingdoms. > By the traditional count from the year 872, the kingdom has existed continuously for 1,145 years, and the list of Norwegian monarchs includes over sixty kings and earls. Norway has both administrative and political subdivisions on two levels: counties and municipalities. The Sámi people have > a certain amount of self-determination and influence over traditional territories through the Sámi Parliament and the Finnmark Act. Norway maintains close ties with both the European Union and the United States. Norway is a founding member of the United Nations, NATO, the European Free Trade Association, the Council of Europe, the Antarctic Treaty, and the Nordic Council; a member of the European Economic Area, the WTO, and the OECD; and a part of the Schengen Area. Norway maintains Nordic welfare model with universal health care and a comprehensive social security system, and Norwegian Society's values are rooted in egalitarian ideals. Defined as a The > XXI century socialism, , the Norwegian state owns key industrial sectors such > as oil (Statoil) or hydropower (Statkraft), having extensive reserves of petroleum, natural gas, minerals, lumber, seafood and fresh water. The petroleum industry accounts for around a quarter of the country's gross domestic product (GDP).";

console.log(deParenthesise(text));
Sebastian Speitel
  • 6,351
  • 2
  • 16
  • 34
1

Using regex, might be tricky for this, as you can have multiple levels of brackets,.. Like you do at the start.

A simple pure Javascript solution though isn't too hard to do..

const txt = `Norway ( ( listen); Norwegian: Norge (Bokmål) or Noreg (Nynorsk); Northern >Sami: Norga), officially the Kingdom of Norway, is a sovereign state and >unitary monarchy whose territory comprises the western portion of the Scandinavian Peninsula plus the remote island of Jan Mayen and the archipelago of Svalbard. The Antarctic Peter I Island and the sub-Antarctic Bouvet Island are dependent territories and thus not considered part of the Kingdom. Norway also lays claim to a section of Antarctica known as Queen Maud Land. Until 1814, the kingdom included the Faroe Islands, Greenland, and > Iceland. It also included Bohuslän until 1658, Jämtland and Härjedalen until > 1645, Shetland and Orkney until 1468, and the Hebrides and Isle of Man until > 1266. Norway has a total area of 385,252 square kilometres (148,747 sq mi) and a population of 5,258,317 (as of January 2017).`;

function removeBrackets(txt) {
  const letters = Array.from(txt);
  const output = [];
  let bcount = 0;
  let pos = 0;
  while (pos < letters.length) {
    const letter = letters[pos];
    if (letter === "(") bcount +=1;    
    if (!bcount) output.push(letter);
    if (letter === ")") bcount -=1;
    pos += 1;
  }
  return output.join("");  
}

console.log(removeBrackets(txt));
Keith
  • 15,057
  • 1
  • 18
  • 31
0

This one removes all " (.....)" from the String.

/([^\(\)]*?)(\s*\([^\(\)]*?\))([^\(\)]*?)/$1$3/g
  • ([^()]*?): Text without the signs '(' and ')' as the first Group ($1)

  • (\s*([^()]*?)):
    Text that can start with a Space (or something like that) (\s),
    then a '('
    than Text without '(' and ')' and
    then a ')' as the second group ($2)

  • ([^()]*?): Text without the signs '(' and ')' as the thirdGroup ($3)

  • [^()]: All Signs except '(' and ')'

  • [^()]*?: Zero or more signs except '(' and ')'

D. Braun
  • 478
  • 1
  • 4
  • 11