簡體   English   中英

如何在正則表達式中的字符串后隔離一定數量的字符

[英]How isolate a certain number of characters after a string in regex

我有這個非常可笑的字符串(請參閱下文),並希望從所有無用的信息中刪除它。

我想隔離所有看起來像這樣的信息:

 Deal: -139.8 | normal: 228.27 | new: 88.47 | CGN - BCN (Sep 25, Sep 27)

這里的數字都可以不同,機場代碼(CGN和BCN)也可以不同。 字符串的長度也可以不同。 而不只是

CGN - BCN (Sep 25, Sep 27)

最后,可能有

CGN - BCN (Sep 25), BCN - NYC (Sep 27)

編輯:如何使用正則表達式來隔離那些信息? 我以[Deal\\:\\s\\-]開頭,以指示字符串應以此開頭。 然后我簡直迷路了。 由於我是regex的新手,所以我不知道如何從這里開始

' ˇˇˇˇ304688Rv0OhVyeiV74 <>‘bD$æ⁄    Deal: -1811.28 | normal: 3158.15 | new: 1346.87 | TXL - NYC (Mar 18), NYC - ATL (Mar 24), ATL - MSY (Mar 30), MSY - TXL (Apr 02)UçÒˇˇˇ¸Uçıˇˇˇˇ304687 <>œ‘vh∞æ⁄    Deal: -319.59 | normal: 624.67 | new: 305.08 | SIN - DUS (Apr 28)UåÕºˇˇˇ¸UåÕøˇˇˇˇ303965lRaD7YP70KR+<team@new.com>œ‘ä√æ⁄    Deal: -319.52 | normal: 624.55 | new: 305.03 | SIN - DUS (Apr 28)UåÕ硡ˇ¸UåÕ롡ˇˇ303966s4Z+<team@new.com>œ‘ù€µæ⁄    Deal: -322.71 | normal: 627.58 | new: 304.87 | SIN - DUS (May 05)Uå¬zˇˇˇ¸Uå¬~ˇˇˇˇ304686 <team@new.com>’‘±êÕæ⁄    Deal: -139.8 | normal: 228.27 | new: 88.47 | CGN - BCN (Sep 25, Sep 27)UåÅØˇˇˇ¸UåÅ≤ˇˇˇˇ3039613//TlCzBs/<>œ‘≈]µæŸ    Deal: -381.52 | normal: 732.66 | new: 351.14 | CGN - PEK (Aug 10)UåaGˇˇˇ¸UåaKˇˇˇˇ303962lWWb2SgeIy+<>œ‘Ÿ∏æŸ    Deal: -148.04 | normal: 293.55 | new: 145.51 | BER - LPA (Oct 17)UåT!'

如果格式固定,則可以使用

\bDeal:.*?\([^|]*\)

並替換為empty string參見演示。

https://regex101.com/r/fM9lY3/55

var re = /\bDeal:.*?\([^|]*\)/gm; 
var str = '\' ˇˇˇˇ304688Rv0OhVyeiV74 <>‘bD$æ⁄    Deal: -1811.28 | normal: 3158.15 | new: 1346.87 | TXL - NYC (Mar 18), NYC - ATL (Mar 24), ATL - MSY (Mar 30), MSY - TXL (Apr 02)UçÒˇˇˇ¸Uçıˇˇˇˇ304687 <>œ‘vh∞æ⁄    Deal: -319.59 | normal: 624.67 | new: 305.08 | SIN - DUS (Apr 28)UåÕºˇˇˇ¸UåÕøˇˇˇˇ303965lRaD7YP70KR+<team@new.com>œ‘ä√æ⁄    Deal: -319.52 | normal: 624.55 | new: 305.03 | SIN - DUS (Apr 28)UåÕ硡ˇ¸UåÕ롡ˇˇ303966s4Z+<team@new.com>œ‘ù€µæ⁄    Deal: -322.71 | normal: 627.58 | new: 304.87 | SIN - DUS (May 05)Uå¬zˇˇˇ¸Uå¬~ˇˇˇˇ304686 <team@new.com>’‘±êÕæ⁄    Deal: -139.8 | normal: 228.27 | new: 88.47 | CGN - BCN (Sep 25, Sep 27)UåÅØˇˇˇ¸UåÅ≤ˇˇˇˇ3039613//TlCzBs/<>œ‘≈]µæŸ    Deal: -381.52 | normal: 732.66 | new: 351.14 | CGN - PEK (Aug 10)UåaGˇˇˇ¸UåaKˇˇˇˇ303962lWWb2SgeIy+<>œ‘Ÿ∏æŸ    Deal: -148.04 | normal: 293.55 | new: 145.51 | BER - LPA (Oct 17)UåT!\'';
var subst = ''; 

var result = str.replace(re, subst);

暫無
暫無

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

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