如何使用ES6类语法分割多段字符串并返回任意字符串

时间:2020-07-29 18:07:45

标签: 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.

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)

这将输出字符串

"On the circles, green statues ride like South American
liberators above the breeding vegetation—"

我不知道如何分割原始多段字符串,以使回车符的格式保持在每首诗的中间,以及如何在每节经文之间的空格/回车符之间进行分割

我想要使用的预期格式是较新的ES6类语法。

谢谢!

诗歌来源:https://www.poetryfoundation.org/poems/53020/july-in-washington

0 个答案:

没有答案