1

For a given URL,

/disconnect/<backend>/foo/<association_id>/

I'd like to get

/disconnect/:backend/foo/:association_id/

There could be any number of <pattern>s in a path.

1
  • If you know the structure of the urls are going to be consistent, you can split the string on slashes, and then get the substring at the index that you want. Commented Aug 5, 2017 at 3:55

3 Answers 3

1

Below is a regex to use with replace method

var str = '/disconnect/<backend>/foo/<association_id>/',
    reg = /<([^>]+)>/g;

console.log(str.replace(reg, ":$1"));

DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

What about this way? Live Demo http://jsfiddle.net/d4N9s/

var mystring = "/disconnect/<backend>/foo/<association_id>/"
var middle = mystring.replace(/>/g , "")
console.log(middle.replace(/</g , ":"));

Cleaner way:

var mapO = {
   '>':"",
   '<':":",
};
str = mystring.replace(/<|>/gi, function(matched){
  return mapO[matched];
});

console.log(str);

Comments

0
/<(.*?)>/g

That will match all instances of a string between < and >. You can use some simple JavaScript to replace each instance pretty easily.

http://regexr.com/3ggen

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.