javascript - 与正则表达式的混淆

标签 javascript regex string replace

下面是一个字符串变量和我的正则表达式,它部分工作,但它删除了之后的所有内容,例如,我只想删除该一个属性...

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

最佳答案

正则表达式的 .* 部分选择所有内容。 [^;]*; 如“[not ;]*;”应该这样做,具体取决于语言,它可能是 [!;]*;

关于javascript - 与正则表达式的混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41430252/

相关文章:

javascript - 语法错误: Unexpected end of input in fetch API

c++ - 用于自追加的字符串反向迭代器的持久性

javascript - 使用 Javascript 重新格式化包含日期的字符串

java - 识别字符串中的特定字符并将其删除 - 不起作用

javascript - 我对过滤器和 map 功能感到困惑。我正在给它们嵌套。请为我指明正确的方向

javascript - Service Worker - 首先是网络然后缓存回退到静态页面

java - 在数亿文本中进行正则表达式搜索的任何建议(存储在 MongoDB 中)

regex - Unix/Linux/FreeBSD 使用 Perl Regex 查找命令

python - 正则表达式在引号内的键后查找字符串

javascript - 带有 typescript 的 AngularJS : this resolved differently in service and config constructor