user824624 user824624 - 2 months ago 26
Javascript Question

use regular expression for this pattern in mqtt topics

Here is a standard topic pattern which is used in mqtt.


How could I use the regular expression to format this topic pattern with a real device name.

I am not very into the regular expression, so what I would need is a function to make a topic given a device name.

for example,

pattern : "lights/hue/{device_name}/get/sensing"
input : name = 'device123'
output: "lights/hue/device123/get/sensing"

Currently I am using the lua, would someone help me?


Assuming lua:

pattern = "lights/hue/{device_name}/get/sensing"
name = "device123"
output = string.gsub(pattern, "{device_name}", name )

There is no need for a regular expression. Simple replacement will do the job.