Javascript 如何在ES6类语法中拆分包含多个段落的字符串并返回任意字符串
我想把这首诗的摘录分成一个数组Javascript 如何在ES6类语法中拆分包含多个段落的字符串并返回任意字符串,javascript,arrays,split,Javascript,Arrays,Split,我想把这首诗的摘录分成一个数组 The stiff spokes of this wheel touch the sore spots of the earth. On the Potomac, swan-white power launches keep breasting the sulphurous wave. Otters slide and dive and slick back their hair, raccoons clean their meat in the creek.
The stiff spokes of this wheel
touch the sore spots of the earth.
On the Potomac, swan-white
power launches keep breasting the sulphurous wave.
Otters slide and dive and slick back their hair,
raccoons clean their meat in the creek.
On the circles, green statues ride like South American
liberators above the breeding vegetation—
prongs and spearheads of some equatorial
backland that will inherit the globe.
The elect, the elected . . . they come here bright as dimes,
and die dishevelled and soft.
在新阵列中,我希望看到如下输出:
[
"The stiff spokes of this wheel
touch the sore spots of the earth.",
"On the Potomac, swan-white
power launches keep breasting the sulphurous wave.",
"[more strings below]"
]
new Poem().targetingMethod(3)
如果我把这首诗作为一个长字符串来接收,我该如何将它分割成一系列子字符串,每个段落都作为自己的字符串
关键是我需要能够任意地以任何子字符串为目标,调用如下:
[
"The stiff spokes of this wheel
touch the sore spots of the earth.",
"On the Potomac, swan-white
power launches keep breasting the sulphurous wave.",
"[more strings below]"
]
new Poem().targetingMethod(3)
哪个将输出字符串
"On the circles, green statues ride like South American
liberators above the breeding vegetation—"
我不知道如何拆分原来的多段字符串来保持回车的格式在每首诗句的中间,以及在每个诗句之间的空白空间/回车符上分裂。
我希望使用的格式是较新的ES6类语法
谢谢
Poem source:要处理边缘情况,可能希望它更像
\n{2,}
将其作为类的一部分有什么关系?如何编写一个类是a)在文档中解释的b)不是你问题的一部分-编辑:现在,我仍然不明白为什么这是一个问题。你试过什么了吗?@ChrisG我更新了我的问题,因为我想了解这种东西在较新的ES6类语法中是如何工作的。这对我来说并不简单。你想上诗歌课吗?然后将原始字符串放入构造函数中@天哪,嗯,这看起来确实像我要问的。这有助于我更好地理解,谢谢!要处理边缘情况,可能希望它更像\n{2,}
将其作为类的一部分有什么关系?如何编写一个类是a)在文档中解释的b)不是你问题的一部分-编辑:现在,我仍然不明白为什么这是一个问题。你试过什么了吗?@ChrisG我更新了我的问题,因为我想了解这种东西在较新的ES6类语法中是如何工作的。这对我来说并不简单。你想上诗歌课吗?然后将原始字符串放入构造函数中@天哪,嗯,这看起来确实像我要问的。这有助于我更好地理解,谢谢!