簡體   English   中英

如何使用 JavaScript 正則表達式拆分此文本?

[英]How to split this text using JavaScript regular expression?

我想拆分此文本。 我正在嘗試使用 JavaScript 正則表達式來做到這一點。

(1) 真的沒有。 (2) 嗯。 (3)看哪,王子(4)是他自然元素的關鍵,畏縮在他生命中女人的擺布之下。 (5) 見我,也許你想和我的女兒們一起吐口水,教她們一些組合。 (6) 陛下,您無疑是最好的老師。 (7) 例如,是我女兒教我現代世界的語言。

我想將其解析為片段組。 我正在尋找這些結果之一。

[
  [1, "Really not."],
  [2, "Uh huh."],
  [3, "Behold Prince"],
]


[
  {id: 1, text: "Really not."},
  {id: 2, text: "Uh huh."},
  {id: 3, text: "Behold Prince"},
]

我使用這種模式。

/\(([0-9])\){1,3}(.+?)\(/g

請問你能幫幫我嗎? 我應該使用什么模式來正確拆分文本?

先感謝您!

您可以在 javascript 中使用 regex 和 string.matchAll function 來做您想做的事

 const str = `(1) Really not. (2) Uh huh. (3) Behold Prince (4) are key in his natural element, cowering at the mercy of the women in his life. (5) See me perhaps you'd like to spout with my daughters and teach them some combination. (6) No doubt you are the best teacher, your majesty. (7) It is my daughter's that teach me in the languages of the modern world, for instance.`; let array = [...str.matchAll(/\(([0-9]+)\)\s*(.*?)\s*(?=$|\()/g)].map(a=>[+a[1],a[2]]) console.log(array)

我使用第四只鳥的正則表達式更新了我的答案,因為它比我寫的正則表達式干凈得多。

而不是匹配(您可以斷言它或字符串的結尾。

這部分\){1,3}表示重復右括號 1-3 次。

如果要匹配 1-3 位數字:

\(([0-9]+)\)\s*(.*?)\s*(?=$|\()
  • \(匹配(
  • ([0-9]+)捕獲組 1中的 1+ 個數字(在代碼中用m[1]表示)
  • \)匹配)
  • \s*匹配可選的空白字符
  • (.*?)第 2 組中捕獲盡可能少的字符(在代碼中用m[2]表示)
  • \s*匹配可選的空白字符
  • (?=$|\()斷言字符串的結尾或(在右側

正則表達式演示

 const regex = /\(([0-9]+)\)\s*(.*?)\s*(?=$|\()/g; const str = `(1) Really not. (2) Uh huh. (3) Behold Prince (4) are key in his natural element, cowering at the mercy of the women in his life. (5) See me perhaps you'd like to spout with my daughters and teach them some combination. (6) No doubt you are the best teacher, your majesty. (7) It is my daughter's that teach me in the languages of the modern world, for instance.`; console.log(Array.from(str.matchAll(regex), m => [m[1], m[2]]));

...一種基於matchAll以及RegExp的方法,它使用命名的捕獲組正向前瞻... /\((?<id>\d+)\)\s*(?<text>.*?)\s*(?=$|\()/g ...

 // see... [https://regex101.com/r/r39BoJ/1] const regX = (/\((?<id>\d+)\)\s*(?<text>.*?)\s*(?=$|\()/g); const text = "(1) Really not. (2) Uh huh. (3) Behold Prince (4) are key in his natural element, cowering at the mercy of the women in his life. (5) See me perhaps you'd like to spout with my daughters and teach them some combination. (6) No doubt you are the best teacher, your majesty. (7) It is my daughter's that teach me in the languages of the modern world, for instance." console.log([...text.matchAll(regX) ].map( ({groups: { id, text }}) => ({ id: Number(id), text }) ) );
 .as-console-wrapper { min-height: 100%;important: top; 0; }

筆記

上述方法不包括在文本片段內出現(允許存在)左括號/ ( 。因此,為了始終處於保存狀態,OP 應考慮基於split / reduce的方法......

 const text = " (1) Really not. (2) Uh (huh). (3) Behold Prince (4) are key in his natural element, cowering at the mercy of the women in his life. (5) See me perhaps you'd like to spout with my daughters and teach them some combination. (6) No doubt you are the best teacher, your majesty. (7) It is my daughter's that teach me in the languages of the modern world, (for instance). " console.log( text.split(/\s*\((\d+)\)\s*/).slice(1).reduce((list, item, idx) => { if (idx % 2 === 0) { list.push({ id: Number(item) }); } else { // list.at(-1).text = item; list[list.length - 1].text = item.trim(); } return list; }, []) ); // test / check... console.log( 'text.split(/\s*\((\d+)\)\s*/)...', text.split(/\s*\((\d+)\)\s*/) );
 .as-console-wrapper { min-height: 100%;important: top; 0; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM