0

I have a $location.path() that is of the following type of format:

/request/add/c3smsVdMHpVvSrspy8Vwrr5Zh8qSyP7q

I am interested in filtering the hash after /request/add/ to the following. As you can see, the last four chars are shown, but everything else before that is labeled as [FILTERED]

/request/add/[FILTERED]yP7q

I did some basic code which converts the hidden chars to #, but I got stuck in trying to apply the string [FILTERED] after the /request/add.

old_path = $location.path()
path = old_path.replace(/.(?=.{4,}$)/g, '#');
theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169

1 Answers1

3

You could use substring. This will give you the [FILTERED] contents then you can do whatever you'd like with them.

var old_path = '/request/add/c3smsVdMHpVvSrspy8Vwrr5Zh8qSyP7q';
var filtered = old_path.substring('/request/add/'.length, old_path.length - 4);
var path = old_path.replace(filtered, '[FILTERED]');
console.log(path);
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
  • This only substrings out that first n-characters, and doesn't replace it with `[FILTERED]`. I would accept your answer if you did that. – theGreenCabbage Sep 08 '16 at 22:16
  • @theGreenCabbage Edited. It's just a matter of replacing what you've extracted with what you actually want. – Mike Cluck Sep 08 '16 at 22:17
  • This is perfect, thanks. I actually have a set of paths like `/request/add/` that I would love the method to rotate through to see if there is a match. For example `/request/remove/`, `/request/update/`, etc. What's the cleanest way to do this? – theGreenCabbage Sep 08 '16 at 22:24
  • @theGreenCabbage That all depends on what you're trying to match. This doesn't "match" anything, it just replaces a section of a string. – Mike Cluck Sep 08 '16 at 22:26
  • The match is just a string match like from the examples I listed. Nothing too fancy. – theGreenCabbage Sep 08 '16 at 22:26
  • @theGreenCabbage So you want to see if the path ends in some kind of hash? Or that it's been filtered? Your examples aren't really matching anything either... Unless you're saying the path could start with `/request/add/`, `/request/remove/` etc. Then that's totally doable. – Mike Cluck Sep 08 '16 at 22:28