2

Javascript regex replace single slash into double slash not for replace double slash in a string?

var tempPath ="//DocumentImages//Invoices//USD//20130425//I27566554 Page- 1.tif&//hercimg/IMAGES/2008/20130411/16192144/16192144-10003.tif&";

Here replace all single slash in to double (//) not to all double slash.

like //DocumentImages//Invoices//USD//20130425//I27566554 Page- 1.tif&//hercimg//IMAGES//2008//20130411//16192144//16192144-10003.tif&

3
  • 1
    Have you tried anything so far? Commented May 23, 2013 at 10:32
  • Yes. var tempPath ="//DocumentImages//Invoices//USD//20130425//I27566554 Page- 1.tif&//hercimg/IMAGES/2008/20130411/16192144/16192144-10003.tif&"; image = tempPath.replace(/\//g,"\/\/"); and i get. ////DocumentImages////Invoices////USD////20130425////I27566554 Page- 1.tif&////hercimg//IMAGES//2008//20130411//16192144//16192144-10003.tif& Commented May 23, 2013 at 10:34
  • I made it now... I am new to stackoverflow... Commented May 23, 2013 at 10:46

3 Answers 3

1

This would work assuming your string does not also end in a /

yourString.replace(/\/[^\/]/g,"//")
  • /stuff/ is just JavaScript regex literal notation
  • \/ is an escaped "/"
  • [^\/] is anything but a "/" (again, with escaping)
  • the "g" on the regex literal means "replace all matches and not just the first"

which we replace for "//" which is what you want.

replace accepts a string and returns a new string with the value changed without changing the original.

Here is a working fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

I expect //DocumentImages//Invoices//USD//20130425//I27566554 Page- 1.tif&//hercimg//IMAGES//2008//20130411//16192144//16192144-10003.tif& Not this ///ocumentImages///nvoices///SD///0130425///27566554 Page- 1.tif&///ercimg//MAGES//008//0130411//6192144//6192144-10003.tif&
0
yourString.replace(/([^\/])\/([^\/])/g,"$1//$2")

2 Comments

@ramesh, please check my updated answer. Sorry, I did not check that the previous one replaced also the 2 chars on the side
Yes ... I noticed after sometime... Thank You again... :)
0

Could be also helpful:

var s = "http://www.some-url.com//path//to";
var res = s.replace(/(https?:\/\/)|(\/)+/g, "$1$2");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.