Use command symbols for pattern matching
masterTo simplify command writing, react-speech-recognition supports special symbols within command strings:
- Splats (
*): Matches multi-word text. Each word/group matched by a splat is passed as a separate argument to the callback.- Example:
'I would like to order *'matches'I would like to order pizza'(wherepizzais passed to the callback).
- Example:
- Named variables (
:<name>): Matches a single word.- Example:
'I am :height metres tall'matches'I am 6 metres tall'(where6is passed to the callback).
- Example:
- Optional words
( ): Phrases wrapped in parentheses are not required to match.- Example:
'Pass the salt (please)'matches both'Pass the salt'and'Pass the salt please'.
- Example:
const commands = [
{ command: 'I am :height metres tall', callback: (height) => console.log(height) },
{ command: 'Order *', callback: (item) => console.log(item) },
{ command: 'Hello (there)', callback: () => console.log('Matched') }
]