3

I'm working on javascript and I have some problem with javascript replace function. Here is my code:

var jpgPath ="../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm201000135.jpg@../Publish
/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001352.jpg@../Publish
/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001353.jpg@../Publish
/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001354.jpg@../Publish       
/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001355.jpg@../Publish    
/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001356.jpg@../Publish   
/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001357.jpg@../Publish
/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001358.jpg@../Publish
/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001359.jpg@../Publish  
/Pdf/Publish_27Jul2011_04-47-09_PM/adfm20100013510.jpg@../Publish
/Pdf/Publish_27Jul2011_04-47-09_PM/adfm20100013511.jpg@../Publish
/Pdf/Publish_27Jul2011_04-47-09_PM/adfm20100013512.jpg";

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

but it's not replacing all the occurrence of ".." with "../..", it's replacing the first match and after that it ignore other matches.

CRM
  • 4,431
  • 2
  • 26
  • 32
Anil
  • 31
  • 1
  • 2

5 Answers5

7

Pass a regex with global flag as first param

jpgPath = jpgPath.replace(/\.\./g, "../..");
Rafael
  • 18,099
  • 5
  • 56
  • 66
0

Try following:

jpgPath = jpgPath.replace(/../g, ”../..”);
arik
  • 25,780
  • 35
  • 94
  • 150
0

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

zatatatata
  • 4,601
  • 1
  • 18
  • 40
0

To do that, you'll need to use the regex and the g (global) operator:

// because . is a special character in regex, you need to escape it
jpgPath = jpgPath.replace(/\.\./g, "../..");
cwallenpoole
  • 76,131
  • 26
  • 124
  • 163
0

Try this.

 var jpgPath ="../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm201000135.jpg@../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001352.jpg@../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001353.jpg@../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001354.jpg@../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001355.jpg@../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001356.jpg@../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001357.jpg@../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001358.jpg@../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm2010001359.jpg@../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm20100013510.jpg@../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm20100013511.jpg@../Publish/Pdf/Publish_27Jul2011_04-47-09_PM/adfm20100013512.jpg";

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

console.log(jpgPath );

http://jsfiddle.net/t8Wp8/

ysrb
  • 6,643
  • 2
  • 28
  • 30