How to use variable slots in a string in node.js?


How to use variable slots in a string in node.js?



I am working in Google Assistant's Action SDK and in my code I want a {location} slot. I have tried this:-




gapp.intent('actions.intent.TEXT', (conv, input) => {
let rawInput = input.toLowerCase();
console.log('USER SAID ' + rawInput);
if(rawInput == `i am in ${loc}`)
{
console.log("User is in " + loc);
conv.ask("Okay so you are in" + loc);
}
}



However if i say "I am in mumbai" in the simulator it doesn't enter this 'if' statement. How can I use the "loc" variable in the user input?





What loc variable? It's not declared or set to a value anywhere in the code you posted. edit oh wait; that's simply not how string templates work. They're for creating strings, not pattern matching.
– Pointy
Jun 30 at 13:29



loc





For simplicity I have declared 'loc' variable global.
– ABHILASHA
Jun 30 at 13:45






What's loc declared as? .. Mumbai or mumbai ? ... note that JS is case sensitive, so "I am in mumbai" won't match "I am in Mumbai". If that's the case, use e.g. .toLowerCase() on both rawInput and i am in ${loc}
– LGSon
Jun 30 at 13:50



loc


Mumbai


mumbai


"I am in mumbai"


"I am in Mumbai"


.toLowerCase()


rawInput


i am in ${loc}





@LGSon it doesn't matter because that is not what string templates do. There's no "matching" at all. Matching is done by regular expressions. String templates are for building string values.
– Pointy
Jun 30 at 13:52






@Pointy Won't rawInput.toLowerCase() == `i am in ${loc}`.toLowerCase() match if they both say i am in mumbai though with different letter casing?
– LGSon
Jun 30 at 13:55



rawInput.toLowerCase() == `i am in ${loc}`.toLowerCase()


i am in mumbai




1 Answer
1



You can use a String.prototype.match() match like so:


String.prototype.match()


let found = rawInput.match(/I am in (.*)/)
if (found) {
let location = found[1];
console.log("User is in " + loc);
}





Yes this works! Thank you!
– ABHILASHA
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter