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,569
  • 4
  • 27
  • 33
Anil
  • 31
  • 1
  • 2

5 Answers5

7

Pass a regex with global flag as first param

jpgPath = jpgPath.replace(/\.\./g, "../..");
Rafael
  • 18,349
  • 5
  • 58
  • 67
0

Try following:

jpgPath = jpgPath.replace(/../g, ”../..”);
arik
  • 28,170
  • 36
  • 100
  • 156
0

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

zatatatata
  • 4,761
  • 1
  • 20
  • 41
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
  • 79,954
  • 26
  • 128
  • 166
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,693
  • 2
  • 29
  • 30