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
  • 110,383
  • 23
  • 155
  • 178
MojtabaSh
  • 627
  • 1
  • 11
  • 23

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
  • 110,383
  • 23
  • 155
  • 178
0

Can you use following:

"../folder1/folder2/image1.jpg".replace("../","")
vijayP
  • 11,352
  • 5
  • 24
  • 38
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