how to match and replace multiple route parameter using angular replace function

Viewed 200

I'm trying to replace multiple parameter using angular replace function but the problem that the function detected the first parameter.

eg: I have this route admin/management/{type}/card/{id}, the route.replace function result admin/management/waiting/%7Bid%7D it work fine with the first parameter.

/**
 * @param route 
 * @param obj 
 * @returns 
 */
  getFormattedRouter(route: String, obj: any) {
    console.log(route);//admin/management/{type}/card/{id}
    return route.replace(/{([a-zA-Z_]+?)}/, function (match, capture) {
      console.log(match);//{type}
      console.log(capture);//waiting
      //detected the first one then stops
      return obj[capture];
      //admin/management/waiting/%7Bid%7D
    });
  }

how can I detect all the parameters in a route.

1 Answers

Your regexp does not seem to be capturing all the matches, you can try this instead /({\w*})/g. Note also the g at the end that indicates the fact that you want to find all the matches, not stop after the first one. Check this link for more info.

Related