gpt4 book ai didi

javascript - Javascript : How to Replace the multiple strings based on conditions

转载 作者:行者123 更新时间:2023-11-29 16:46:30 26 4
gpt4 key购买 nike

下面的字符串是名称和值的格式('名称' - '值';)。如何删除出现的值 '0 bytes' 及其名称和删除名称 '/platform/sun4v/lib/sparcv9/libc_psr.so.1' 及其值。

/devices - 0 bytes; /system/object - 0 bytes; /etc/dfs/sharetab - 0 bytes; /dev/fd - 0 bytes; /var/run - 13.77 GiB; /platform/sun4v/lib/sparcv9/libc_psr.so.1 - 27.24 GiB; / - 27.24 GiB; /var - 26.84 GiB; /proc - 0 bytes; /system/contract - 0 bytes; /rpool - 18.04 GiB; /mnt - 157.67 TiB; /vol - 0 bytes; /was8 - 48.26 GiB; /platform/sun4v/lib/libc_psr.so.1 - 27.24 GiB; /etc/svc/volatile - 13.77 GiB; /etc/mnttab - 0 bytes; /tmp - 13.82 GiB;

最佳答案

在分号上拆分字符串并根据您的条件过滤数组,然后加入数组以添加回分号并转换回一个字符串:

var str = '/devices - 0 bytes; /system/object - 0 bytes; /etc/dfs/sharetab - 0 bytes; /dev/fd - 0 bytes; /var/run - 13.77 GiB; /platform/sun4v/lib/sparcv9/libc_psr.so.1 - 27.24 GiB; / - 27.24 GiB; /var - 26.84 GiB; /proc - 0 bytes; /system/contract - 0 bytes; /rpool - 18.04 GiB; /mnt - 157.67 TiB; /vol - 0 bytes; /was8 - 48.26 GiB; /platform/sun4v/lib/libc_psr.so.1 - 27.24 GiB; /etc/svc/volatile - 13.77 GiB; /etc/mnttab - 0 bytes; /tmp - 13.82 GiB;'

var splitStr = str.split(';');
var newStr = splitStr.filter(function(item){
var itemSplit = item.split('-');

return !(itemSplit != '' && (itemSplit[0].trim() === '/platform/sun4v/lib/sparcv9/libc_psr.so.1' || itemSplit[1].trim() ==='0 bytes'));
}).join(';');

//newStr is will contain the filtered string
console.log(newStr);

关于javascript - Javascript : How to Replace the multiple strings based on conditions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40631174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com