2011-07-27 96 views
2

我正在致力於JavaScript,我有一些與JavaScript替換功能的問題。 這裏是我的代碼:Javascript替換不工作

var jpgPath ="../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish 
/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish 
/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish 
/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish  
/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish  
/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish 
/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish 
/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish 
/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish 
/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish 
/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish 
/Pdf/Publish_27Jul2011_04-47-09_PM/adfm20100013512.jpg"; 

jpgPath = jpgPath.replace("..", "../.."); 

,但它不是以「../ ..」,它取代了第一場比賽替換所有發生的「」之後,它忽略了其他比賽。

回答

7

傳遞與全局標誌正則表達式作爲第一個參數

jpgPath = jpgPath.replace(/\.\./g, "../.."); 
0

嘗試以下操作:

jpgPath = jpgPath.replace(/../g, 」../..」); 
0

運行jpgPath = jpgPath.replace(/\.\./g, "../..");來代替。

0

爲了做到這一點,你需要使用正則表達式和g(全球)運營商:

// because . is a special character in regex, you need to escape it 
jpgPath = jpgPath.replace(/\.\./g, "../.."); 
0

試試這個。

var jpgPath ="../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish/Pdf/Publish_27Jul2011_04-47-09_PM/[email protected]/Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm20100013512.jpg"; 

jpgPath = jpgPath.replace(/\.\./g, "../.."); 

console.log(jpgPath); 

http://jsfiddle.net/t8Wp8/