I have a problem. I want to make an API on JS. Here's what it looks like.

The user enters a request to be applied:

var req = "msg:{name}:{message}" // Пример запроса: "msg:john:Hello, John" 

The user can enter more parameters. The whole point is that you first need to find out what the keys are for the parameters and then ask for the value.

And I need to have something like this at the output:

 { name: "John", msg: "Hello, John" } 
  • Forgive me a little not correctly written msg, but message - Nikita Shamberger

1 answer 1

split option:

 var req = "msg:john:Hello, John"; var spl = req.split(':'); var data = { name: spl[1], message: spl[2] }; console.log(data); 

Regular Expression Option:

 var req = "msg:john:Hello, John"; var found = req.match(/msg:(.+):(.+)/); var data = { name: found[1], message: found[2] }; console.log(data);