2

How can I replace duplicated 'slash' from a string?

For instance,

str = '/estate//info//';  
alert(fragment.replace(/\/\/+/, "/"));

result,

/estate/info//

But I am after,

/estate/info/

3 Answers 3

9

Try this:

str = '/estate//info//';  
alert(str.replace(/\/\/+/g, "/"));  
// where 'g' will do the global search and replace it with single '/'
Sign up to request clarification or add additional context in comments.

Comments

3

try this,

str = '/estate//info//';  
alert(fragment.replaceAll("//", "/"));

1 Comment

will alert /estate/info//.
1

You can also try

  var val = "\\val1\\val2\\val3";
  val = val.substr(0, val.lastIndexOf("\\"));
  alert(val);

Fiddle

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.