我有一個文本需要捕獲第二組
Response on your Property Listing
Dear Rahul Bond,
A user is interested in your Property, ID 62455995: 2 BHK , Multistorey
Apartment in Raheja Vihar , Mumbai.
Details of Contact Made:
Sender's Name: Test (Individual)
要檢索的文本是2 BHK , Multistorey Apartment in Raheja Vihar , Mumbai。這確實以 **A 用戶對您的屬性感興趣,ID xxxxxxx 開頭:**
A user is interested in your Property, ID 62455995: 2 BHK , Multistorey
Apartment in Raheja Vihar , Mumbai.
預期產量為2 BHK ,位于孟買 Raheja Vihar 的多層公寓。
我嘗試過.match(/(A user is interested in your Property, ID.*[^:]) (.*\n.*)/g)
不幸的是它給了我所有 2 行文本。
uj5u.com熱心網友回復:
您可以使用此正則運算式:
const text = `Response on your Property Listing
Dear Rahul Bond,
A user is interested in your Property, ID 62455995: 2 BHK , Multistorey
Apartment in Raheja Vihar , Mumbai.
Details of Contact Made:
Sender's Name: Test (Individual)`;
const rgx = /(?<=A user is interested in your Property, ID \d : )(.*\n.*)/g;
const result = text.match(rgx);
console.log(result);
它在這里作業https://regexr.com/6tulh
uj5u.com熱心網友回復:
從上面的評論...
“更一般地尋找 ...
ID,然后是一個空格(序列),然后是一個 8 位數字的序列,然后是一個冒號,然后是另一個空格(序列),然后捕獲任何不是點 /包括第一個出現的點/句點的句點.../ID\s \d{8}\:\s (?<description>[^.] \.)/g"
...并利用建議的正則運算式...
const multilineText =
`Response on your Property Listing
Dear Rahul Bond,
A user is interested in your Property, ID 62455995: 2 BHK , Multistorey
Apartment in Raheja Vihar , Mumbai.
Details of Contact Made:
Sender's Name: Test (Individual)`;
// see ... [https://regex101.com/r/uDFHaV/1]
const regX = /ID\s \d{8}\:\s (?<description>[^.] \.)/g;
// either ...
console.log(
[...multilineText.matchAll(regX)]
.map(({ groups: { description } }) => description)
);
// or ...
console.log(
[...multilineText.matchAll(regX)]
[0]?.groups.description
);
// ... or ...
console.log(
regX
.exec(multilineText)
?.groups.description
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/507576.html
標籤:javascript 正则表达式
上一篇:Mongo聚合;匹配欄位的子欄位至少有一個大于指定值的陣列元素
下一篇:如何僅洗掉表行的最后tds/列?
