2016-11-16 101 views
2

字符串下面的格式爲名稱和值('name' - 'value';)。 如何刪除值'0 bytes'及其名稱的出現以及 刪除名稱'/platform/sun4v/lib/sparcv9/libc_psr.so.1'及其值。Java腳本:如何根據條件替換多個字符串

/devices - 0 bytes;/system/object - 0個字節;/etc/dfs/sharetab - 0 bytes;/dev/fd - 0字節;/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字節;/system/contract - 0字節;/rpool - 18.04 GiB;/mnt - 157.67 TiB;/vol - 0字節;/was8 - 48.26 GiB; /platform/sun4v/lib/libc_psr.so.1-27.24 GiB;/etc/svc/volatile - 13.77 GiB;/etc/mnttab - 0字節;/tmp - 13.82 GiB;

回答

3

分割上selicolon字符串和濾鏡陣列根據您的標準,然後再加入陣列重新添加分號和轉換回一個字符串:

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

你可以在這裏使用正則表達式來轉換字符串。

參見:

  1. 第一正則表達式(/\/platform\/sun4v\/lib\/sparcv9\/libc_psr\.so\.1 [^\;]+\;\s/)選擇長,唯一子;
  2. 第二正則表達式(/\/[^\-]+- 0 bytes\;\s/g)選擇(和每個實例的前面的標籤)的- 0 bytes所有實例

var paragraphAfter = document.getElementsByClassName('after')[0]; 
 
var textBefore = document.getElementsByClassName('before')[0].textContent; 
 
var textAfter = textBefore.replace(/\/platform\/sun4v\/lib\/sparcv9\/libc_psr\.so\.1 [^\;]+\;\s/,''); 
 
var textAfter = textAfter.replace(/\/[^\-]+- 0 bytes\;\s/g,''); 
 
paragraphAfter.textContent = textAfter;
<h2>Before:</h2> 
 
<p class="before">/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; /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;</p> 
 

 
<h2>After:</h2> 
 
<p class="after"></p>

1

嘗試此。

str.split(';').filter(item => !item.includes('0 bytes') && !item.includes('/platform/sun4v/lib/sparcv9/libc_psr.so.1')).join(';') 
0

我相信你仍然希望你的輸出格式相同。這應該爲你工作:

function formatInfo(data) { 
    return data.split(";").filter(function(item,i) { 
     if(item.indexOf('0 bytes') >= 0 || item.indexOf('/platform/sun4v/lib/sparcv9/libc_psr.so.1') >= 0) { 
      return null; 
     } 
     return item.trim(); 
    }).join(";").trim(); 
} 

//用法:

formatInfo('/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/run - 13.77 GiB;/- 27.24 GiB; /var - 26.84 GiB; /rpool - 18.04 GiB; /mnt - 157.67 TiB; /was8 - 48.26 GiB; /platform/sun4v/lib/libc_psr.so.1 - 27.24 GiB; /etc/svc/volatile - 13.77 GiB; /tmp - 13.82 GiB" 

乾杯.. !!