2
var stringVal = "../folder1/folder2/image1.jpg";   
stringVal = stringVal.replace(/[../]/g , "");

It will replace all / of string, but I want just replace ../.

How can do that?

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
MojtabaSh
  • 627
  • 1
  • 11
  • 26

3 Answers3

0

You can use regex \.{2}\/

Regular expression visualization

[../] is a character class that means any character from . or /

var stringVal = "../folder1/folder2/image1.jpg";
stringVal = stringVal.replace(/\.{2}\//g, "");

document.write(stringVal);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

Can you use following:

"../folder1/folder2/image1.jpg".replace("../","")
vijayP
  • 11,432
  • 5
  • 25
  • 40
0

You could do it this way if you don't want to get into the regex stuff.

stringVal.split("../").join("")

otherwise you'll need to escape the control characters in your regular expression. Here's another answer that should help: Replacing all occurrences of a string in JavaScript

Community
  • 1
  • 1
heya
  • 1
  • 1