2017-01-02 57 views
0

下面是一個字符串變量,我的正則表達式,它的部分工作,但它刪去,我想只是刪除例如一個財產......混亂與正則表達式

tString='#a5af3c17e39f06875fbe9153d2d66d552{animation-delay: 0s; animation-direction: normal; animation-duration: 0s; animation-fill-mode: none; animation-iteration-count: 1; animation-name: none; animation-play-state: running; animation-timing-function: ease; background-attachment: scroll; background-blend-mode: normal; background-clip: border-box; background-color: rgba(0, 0, 0, 0.498039); background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: repeat; background-size: auto; border-bottom-color: rgb(39, 48, 57); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-bottom-style: none; border-bottom-width: 0px; border-collapse: separate; border-image-outset: 0px; border-image-repeat: stretch; border-image-slice: 100%; border-image-source: none; border-image-width: 1; border-left-color: rgb(39, 48, 57); border-left-style: none; border-left-width: 0px; border-right-color: rgb(39, 48, 57); border-right-style: none; border-right-width: 0px; border-top-color: rgb(39, 48, 57); border-top-left-radius: 0px; border-top-right-radius: 0px; border-top-style: none; border-top-width: 0px; bottom: 0px; box-shadow: rgba(0, 0, 0, 0.4) 0px 2px 20px 0px; box-sizing: border-box; break-after: auto; break-before: auto; break-inside: auto; caption-side: top; clear: none; clip: auto; color: rgb(39, 48, 57); content: ; cursor: auto; direction: ltr; display: block; empty-cells: show; float: none; font-family: Roboto, sans-serif; font-kerning: auto; font-size: 18px; font-stretch: normal; font-style: normal; font-variant: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: normal; font-weight: normal; height: 1110px; image-rendering: auto; isolation: auto; left: 0px; letter-spacing: normal; line-height: 32.4px; list-style-image: none; list-style-position: outside; list-style-type: disc; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; mix-blend-mode: normal; object-fit: fill; object-position: 50% 50%; offset-distance: 0px; offset-path: none; offset-rotation: auto 0deg; opacity: 1; orphans: 2;}' 
 

 
regStr=/animation-direction:.* ;/gm 
 

 
result=tString.replace(regStr,'') 
 

 
console.log(result);

+3

你需要非貪婪的正則表達式,即'。*?' – Fallenhero

+0

你的正則表達式匹配任何以'animation-direction:'開頭的東西,''*'表示**所有東西* *包含之後的匹配(到字符串的末尾)。你可能會對所有內容進行匹配,除了*;字符以確保它在CSS規則結尾處結束選擇。 – ceejayoz

+2

查看https://regex101.com/r/OnTJmx/1 –

回答

2

您的正則表達式的.*部分選擇所有內容。如「[not;] *;」中的「[^;]*;」應該這樣做,這取決於它可能的語言[!;]*;