Home > Backend Development > PHP Tutorial > javascript - 如何匹配"height:12px;width:56px;background:#fff;"中间的"width:56px;"?

javascript - 如何匹配"height:12px;width:56px;background:#fff;"中间的"width:56px;"?

WBOY
Release: 2016-06-06 20:38:30
Original
972 people have browsed it

其中":56px"是未知的,我结果希望replace之后是这样的height:12px;background:#fff;,下面是我写的,会把width后面的全部删掉的

<code>var str = "height:12px;width:56px;background:#fff;";
str.replace(RegExp('width.*;'),'');
//=>height:12px;
</code>
Copy after login
Copy after login

回复内容:

其中":56px"是未知的,我结果希望replace之后是这样的height:12px;background:#fff;,下面是我写的,会把width后面的全部删掉的

<code>var str = "height:12px;width:56px;background:#fff;";
str.replace(RegExp('width.*;'),'');
//=>height:12px;
</code>
Copy after login
Copy after login

str.replace(RegExp('width[^;]*;'),'');

str.replace(RegExp('width:\\w*;'), '');

<code>var str = "height:12px;width:56px;background:#fff;";
str.replace(RegExp('width.*?;'),'');
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template