1

Is there any way (maybe a regex) to replace the id dynamic to "id" I use js and typescript The original url:

localhost:4200/reservations/5eda023aed43c1e46d6ce804/properties/5ca199fdda8ba619695b5728

The url i want:

localhost:4200/reservations/id/properties/id
Ran Marciano
  • 1,254
  • 5
  • 7
  • 25
Dwayne123
  • 39
  • 7

4 Answers4

2

Here's a solution using regex:

function replaceURL(url, data) {
  Object.entries(data).forEach(([key, value]) => url = url.replace(new RegExp(`(?<=${key}\\/)\\w+`, 'g'), value));
  return url;
}

// the original url
const url = 'localhost:4200/reservations/5eda023aed43c1e46d6ce804/properties/5ca199fdda8ba619695b5728';

// put what you want to replace in this object
const data = {
  reservations: 'id',
  properties: 'id'
};

const result = replaceURL(url, data);
console.log(result);

Edit

I think I overthought the situation, if you just want to replace the text similar to 5eda023aed43c1e46d6ce804, you can just use the following regex:

/(?<=\/)[0-9a-f]{24,}/g

const regex = /(?<=\/)[0-9a-f]{24,}/g;

let url = 'localhost:4200/reservations/5eda023aed43c1e46d6ce804/properties/5ca199fdda8ba619695b5728';
let result = url.replace(regex, 'id');
console.log(result);

url = 'http://localhost:4200/minibar/items/5efdcee02c37c160e8a5bbe1';
result = url.replace(regex, 'id');
console.log(result);
Hao Wu
  • 12,323
  • 4
  • 12
  • 39
  • it so good bro, but it dont work for other url like `http://localhost:4200/minibar/items/5efdcee02c37c160e8a5bbe1` Is there a way generality to i can use with any url.So many thanks bro – Dwayne123 Nov 05 '20 at 08:36
  • It works, just make the `data = { items: 'id' }` – Hao Wu Nov 05 '20 at 08:42
  • The key names of the object correspond to the url path name, you have to make them identical. – Hao Wu Nov 05 '20 at 08:43
1

If your url format is fix then you can use this simple code.

var link=" localhost:4200/reservations/5eda023aed43c1e46d6ce804/properties/5ca199fdda8ba619695b5728";
//var link="http://localhost:4200/minibar/items/5efdcee02c37c160e8a5bbe1"
var hasNumber = /\d/; 
var linkParts=link.split("/");
var i=link.indexOf("http")==0?3:2;
for(i;i<linkParts.length;i++)
{
  if(linkParts[i].length>20 && hasNumber.test(linkParts[i]))
    linkParts[i]="id";
}
document.write(linkParts.join("/"));
Yuvraj Mule
  • 387
  • 2
  • 9
  • actually, it's so many url with difference format, it good but not work with another url i have like `http://localhost:4200/minibar/items/5efdcee02c37c160e8a5bbe1` bro, is there anything way to automatic replace.Btw thanks – Dwayne123 Nov 05 '20 at 08:33
  • If you have many different URL then you can split URL by "/" , then loop return array from index 3 (e.g. for i=3 to array.length) ,and check if any part of URL contains number and check its length too and replace with "id" , again join your final array with "/". may this will work for your all URL. – Yuvraj Mule Nov 05 '20 at 09:21
  • @DuongTran check my updated code this may help you. – Yuvraj Mule Nov 05 '20 at 09:40
  • 1
    it's works bro.Thanks – Dwayne123 Nov 05 '20 at 10:14
1

You can try this

let url = 'localhost:4200/reservations/5eda023aed43c1e46d6ce804/properties/5ca199fdda8ba619695b5728';
let replacedUrl = url.replace(/\/[a-fA-F0-9]{24}/g, "/id");
console.log(replacedUrl);
0

You can simply replace the exact id with what you want.

let url = 'localhost:4200/reservations/5eda023aed43c1e46d6ce804/properties/5ca199fdda8ba619695b5728';
let id = '5eda023aed43c1e46d6ce804' // the id to replace

let rx = new RegExp(id,"g"); // use "g" to indicate it should should ALL ids in the string
url = url.replace(rx, "id") // this will replace the dynamic id with the word "id"

// localhost:4200/reservations/id/properties/id
Kevin Potgieter
  • 375
  • 4
  • 13