删除两个括号之间的字符串Javascript

时间:2018-03-21 12:39:46

标签: javascript regex wikipedia

所以我试图在这个维基百科页面上删除所有括号及其内容。我尝试使用.replace(/(.*)/g, '')。结果是一个空字符串。所以我尝试不使用g部分。然后我失去了文本开头的很大一部分。我也尝试用一个单词替换括号,例如Red,结果是:

  

RedRed RedRed RedRed RedRed RedRed RedRed

我想知道你们中是否有人知道这里发生了什么以及你们是否有解决方案。

以下是文字:

  

挪威((收听);挪威语:Norge(Bokmål)或Noreg(Nynorsk); Northern> Sami:Norga),正式为挪威王国,是一个主权国家和单一君主制,其领土包括西部的   斯堪的纳维亚半岛加上扬马延岛的偏远岛屿   斯瓦尔巴群岛。南极彼得一世岛和南极洲   布维岛是附属领土,因此不被认为是其中的一部分   王国。挪威还声称有一部分被称为女王的南极洲   莫德兰德。直到1814年,该王国包括法罗群岛,格陵兰岛,以及>冰岛。它还包括直到1658年的Bohuslän,Jämtland和Härjedalen,直到> 1645年,设得兰群岛和奥克尼群岛直到1468年,赫布里底群岛和马恩岛直到> 1266.挪威总面积为385,252平方公里(148,747平方英里)   人口5,258,317(截至2017年1月)。该国份长期>与瑞典东部边境(1,619公里或1,006英里长)。挪威与>接壤。芬兰和俄罗斯东北部,斯卡格拉克海峡南部,>与丹麦在另一边。挪威拥有广阔的海岸线,面向>北大西洋和巴伦支海。达诺 - 德国国王哈拉尔五世   House ofGlücksburg是现任挪威国王。 Erna Solberg成为Prime>部长于2013年,并于2017年9月再次当选.Erna Solberg取代了> Jens Stoltenberg是2000-2001至2005-2013期间的总理。 >君主立宪制,挪威在议会之间划分国家权力,>根据1814年宪法确定的内阁和最高法院。   这个王国是由大量小王国合并而成立的。 >根据872年的传统统计,王国已经存在   连续1145年,挪威君主名单包括   六十多位国王和伯爵。挪威既有行政也有政治   两个层次的细分:县和市。萨米人有>一定程度的自我决定和对传统的影响   通过萨米议会和芬马克法案的领土。挪威   与欧盟和美国保持密切联系。   挪威是联合国,北约,欧洲自由联盟的创始成员   贸易协会,欧洲委员会,南极条约和   北欧理事会;欧洲经济区,WTO和WTO的成员   经合组织;和申根区的一部分。挪威维持北欧福利模式   全民医疗保健和全面的社会保障体系   挪威社会的价值观植根于平等主义的理想。定义为>二十一世纪的社会主义,挪威国家拥有这样的关键工业部门。作为石油(Statoil)或水力发电(Statkraft),具有广泛的储量   石油,天然气,矿物,木材,海鲜和淡水。该   石油工业约占该国总产值的四分之一   国内产品(GDP)。

3 个答案:

答案 0 :(得分:2)

您必须通过匹配不包含任何其他括号的括号对从内到外删除括号,然后重复该过程,直到不再有对为止:

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));

答案 1 :(得分:1)

使用正则表达式可能会很棘手,因为你可以有多个级别的括号,......就像你在开始时那样。

一个简单的纯Javascript解决方案虽然不是很难做到。

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));

答案 2 :(得分:0)

这个从字符串中删除所有“(.....)”。

/([^\(\)]*?)(\s*\([^\(\)]*?\))([^\(\)]*?)/$1$3/g
  • ([^()] *?):没有标记'('和')'的文字作为第一组($ 1)

  • (适用\ S *([^()] *)?)
    可以以Space(或类似的东西)开头的文本(\ s),
    那么一个'(' 比没有'('和')'和
    的文字 然后a')'作为第二组($ 2)

  • ([^()] *?):没有标记'('和')'作为第三组($ 3)的文字

  • [^()] :除'('和')'之外的所有标志

  • [^()] *?:零以上的标志除了'('和')'