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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:38:30992browse

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

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

回复内容:

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

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

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

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

<code>var str = "height:12px;width:56px;background:#fff;";
str.replace(RegExp('width.*?;'),'');
</code>
Statement:
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