-1

I don't know if this is possible, i'm making api than users send a message from front end page and if this massage have like "hi my name is @name" , i need change this @name to some variable than have his name.

const name = "Paul"
const city = "Boston"
const userMessage = "Hi my name is @name from @city" 

/** I need create new variable changing @name and @city to recpective variable */
const newMessage = "Hi my name is Paul from Boston"

1 Answers1

-1

I would do that as following:

Get @... with match():

var city = userMessage.match("@city")[0];
var name = userMessage.match("@name")[0];

Replace @:

city = city.replace("@","");
name = name.replace("@","");

Creating new message:

var newMessage = "Hi " + name + "from " + city;