Something like that? This uses a regex to filter out unwanted strings.
var inputs = [ "www.site.com/#mp-1", "www.site.com#mp-1", "www.site.com/#mp-1#pic" ]; inputs = inputs.map(function(input) { return input.replace(/#mp-1?/, ''); }); console.log(inputs);
Output:
["www.site.com/", "www.site.com", "www.site.com/#pic"]
jsfiddle: https://jsfiddle.net/tghuye75/
The regular expression that I used /#mp-1?/
Deletes any lines like #mp-
or #mp-1
. For a string of unknown length until the next hashtag, you can use /#mp-[^#]*
, which removes #mp-
, #mp-1
and #mp-somelongstring
.
source share