如果内容只包含一个数字,如何删除'‘和'’以及其中的内容?
起始字符串:
let cityInfo = "Munich (/ˈmjuːnɪk/ MEW-nik; German: München [ˈmʏnçn̩] (listen);[3] Austro-Bavarian: Minga [ˈmɪŋ(ː)ɐ]; Latin: Monachium) is the capital and most populous city of Bavaria. With a population of around 1.5 million,[4] it is the third-largest city in Germany."一些信息之间的‘’我需要保留。例如:ˈmʏnçn̩和mɪŋ(ː)ɐ其中一些我需要删除。示例:3和4
我如何遍历字符串,去掉包含数字的括号,而保留不包含数字的括号?
期望输出:“慕尼黑(/ˈmjuːnɪk/ MEW-nik;德语: Münchenː;奥巴伐利亚语:m Mingaˈmɪŋ(ː)ɐ;拉丁语:m Mingaˈmɪŋ(ː)ɐ)是巴伐利亚州的首府和人口最多的城市。人口约为150万,是德国第三大城市。”
我在网上看了一些参考资料,但我找到的所有参考资料似乎都没有提供我想要做的事情的解决方案。无法工作的示例:how can i remove chars between indexes in a javascript string - Replacing any content inbetween second and third underscore - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split - https://www.digitalocean.com/community/tutorials/how-to-index-split-and-manipulate-strings-in-javascript - https://www.geeksforgeeks.org/how-to-remove-a-character-from-string-in-javascript/
发布于 2019-12-09 09:27:41
您可以将.replace()与正则表达式/\[\d+\]/g一起使用,以匹配方括号中的所有数字序列,然后可以将其替换为空字符串(''):
const cityInfo = "Munich (/ˈmjuːnɪk/ MEW-nik; German: München [ˈmʏnçn̩] (listen);[3] Austro-Bavarian: Minga [ˈmɪŋ(ː)ɐ]; Latin: Monachium) is the capital and most populous city of Bavaria. With a population of around 1.5 million,[4] it is the third-largest city in Germany.";
const res = cityInfo.replace(/\[\d+\]/g, '');
console.log(res);
https://stackoverflow.com/questions/59241433
复制相似问题