javascript - 如何在正则表达式中隔离字符串后的一定数量的字符

标签 javascript 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\-] 开始指示字符串应以此开头。然后我就迷路了。因为我对正则表达式非常陌生,所以我不知道如何从这里继续

' ˇˇˇˇ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:.*?\([^|]*\)

并替换为空字符串。请参阅演示。

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);

关于javascript - 如何在正则表达式中隔离字符串后的一定数量的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34584894/

相关文章:

javascript - 将来自 AJAX 请求的数据序列化为 PHP

javascript - 用 Javascript Regex 替换不包含特定字符串的行的回车符

javascript - 正则表达式测试无法正常工作

python - 如何在python标点符号之前而不是标点符号之后删除空格

javascript - 无需键即可调用 JSON 的元素

javascript - 将鼠标悬停在带有数据表的文本上

javascript - 使用 jquery mobile 定位链接

javascript - Javascript 中的十进制度到度分和秒

Java - 正则表达式用于分割运算符的数学表达式,排除括号内的运算符

javascript - 具有多个单词的正则表达式(以任何顺序)没有重复