I would like to moderate forum where I'm promoted, I want remove part of links who actually redirect links.I know regular expression for it but I've never wrote any script so I need a little help.
It looks like this, I have
http://redirect.com/rd/ad/MYLINK.org/variable
and I would like to remove that fist part http://redirect.com/rd/ad/ to get
Maybe PHP replacement would work for me but I still don't know how to make it working.
Variable part is \w+ and I have many links ending with different group of alphanumeric characters that makes them difficult to define in string search.
I believe it can be done with JavaScript replacement and also in html or in php, but everything I tried didn't work for me.
As I said I've tried to put this code in webpage php body
<script type="text/javascript">
var str = 'http://redirect.com/rd/ad/MYLINK.org/\w+';
var res = str.replace("redirect.com/rd/ad/", "");
</script>
but it didn't work. I had to define that variable part of string as \w+ because I have many links to replace and they are different in that last part I named as "variable".
Any help is appreciated.
</div>
If 'redirect.com/rd/ad/' is consistently the part that you want to remove, you can just use the replace
function.
let url = "http://redirect.com/rd/ad/MYLINK.org/variable";
let fixedUrl = url.replace('redirect.com/rd/ad/','');
console.log(fixedUrl);
If the first part of the url can change, something like this would work
let url = "http://redirect.com/rd/ad/MYLINK.org/variable";
let firstIndex = url.indexOf('MYLINK.org');
let linkString = url.substring(firstIndex);
console.log('http://' + linkString);
What that does is finds where MYLINK.org starts, and cuts off everything before that point with "substring".