0

New to jQuery & JavaScript.

I have

var x = location.pathname; 

(ex: /abc/collection/tea/green/index.php)

Like this I have various pathname retrieved using location.pathname.

I want to replace all "/" in the pathname with ":" (I mean a / with a :) and also I don't want the .php which is at the end. Any help please.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kishoremcp
  • 39
  • 1
  • 3

1 Answers1

-1

This has nothing to do with jQuery. You can use JavaScript to replace your string.

Like this:

var x = location.pathname;
x = x.replace(/\//g, ":");

or just

var x = location.pathname.replace(/\//g, ":");

You can also use the same method to remove the ".php" by adding this:

x = x.replace(/\.php$/i, "");

(assuming you only need to replace it once at the end)

Basically you have to use the regex version of str.replace() and the g (global) switch to do a replace all. Use the str.replace(/texthere/g, "replacement") pattern to replace more than one occurrence - just remember to properly escape 'texthere' so characters don't conflict.

James Wilkins
  • 6,836
  • 3
  • 48
  • 73