2014-05-12 41 views
0

有一個BRAINFREEZE簡單正則表達式替換所有但

我有以下字符串:「pvtVal ROW1 COL3這是一個測試」

如何擺脫任何不pvtVal行\ d +或山坳\ d +

因此,例如:

var test="pvtVal row1 col3 this is a test".replace(/(^(pvtVal |row\d+ |col\d+))/g, ''); 

遺憾的是它不工作。

感謝

+1

您可以使用'match'和'而是join'。 – zneak

回答

2

而不是替換,你可以做到這一點使用String.match()Array.join()

var teststr = 'pvtVal row1 col3 this is a test', 
    matches = teststr.match(/(?:pvtVal|row\d+|col\d+)/g), 
    results = matches.join(' '); 

console.log(results); // => "pvtVal row1 col3" 
+0

Jah比我的好。今天陷入一種思維模式,一切看起來都像釘在我的錘子上。 :) +1 – zx81