[英]Matching paragraph with line breaks Regex
我可以使用什么正則表達式來匹配段落(包括換行符),所以當我使用split()
時,我會得到一個數組,每個句子都作為一個元素?
像這樣的東西:
const paragraph = `
one potatoe
two apples
three onions
`;
const arr = paragraph.split(/(.+?\n\n|.+?$)/);
我有那個返回["one potatoe↵two apples↵", "three onions", ""]
的正則表達式,但我要找的是["one potatoe", "two apples", "three onions"]
。
謝謝您的幫助!
編輯:
每個句子由換行符分隔。 所以在one potatoe
之后有一個換行符(點擊返回)然后是two apples
,換行符和three onions
我了解您希望每一行都帶有與后面一樣多的相鄰換行符的文本。
使用match
而不是split
會更容易:
const paragraph = ` one potatoe two apples three onions`; const arr = paragraph.match(/^.+$[\n\r]*/gm); console.log(arr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.