0

I want to replace all instances of ./ in my js

My code is like this

var text = './test';
var baseUrl = 'http://www.example.com/';
text = text.replace(/./\/g, baseUrl);

This isn't working as I want to also replace a forward slash. How can I ignore this?

brabster
  • 42,504
  • 27
  • 146
  • 186
peter flanagan
  • 9,195
  • 26
  • 73
  • 127

1 Answers1

4

Try to match the dot like \. and the forward slash like \/:

If you want to match a literal dot you have to escape it using a backslash or else it would match (Almost) Any Character.

You have to escape the forward slash \/ because that is the delimiter used for the start and the end of the regex.

var text = './test';
var baseUrl = 'http://www.example.com/';
text = text.replace(/\.\//g, baseUrl);
console.log(text);
The fourth bird
  • 154,723
  • 16
  • 55
  • 70