我有以下状态图,用于验证字符串是否与格式匹配。

我想在xstate中重新创建这个匹配fsm的语言。对于ac、abc、abbbbbc等,机器应返回true,否则返回false。它的输出应该与regex表达式string.match(/^ab*c$/) !== null;完全相同。
我目前有以下情况:
const machine = Machine({
initial: 'start',
states: {
start: {
on: {A: 'state1'}
},
state1: {
on: {
B: 'state1',
C: 'end'
}
},
end: {}
}
});是否有方法通过操作和上下文,我可以检查一个字符串是否匹配这个语言,fsm,通过它到达结束状态。
当然,我可以使用regex,但是为了这个问题,我已经简化了这个问题。我想解析的实际语言不是常规的,需要pda / context。我只想知道使用xstate进行语言/字符串解析的结构。
发布于 2021-11-16 22:34:42
const checkRegex =(content,event)=>{
if(event.match(/^ab*c$/) !== null){
context.valid =true;
}
}
const regexMachine = Machine({
initial: 'state1',
states: {
state1:{
on:{
VALIDATE: {
target:"state2"
}
}
}
state2: {
invoke:{
src:(context,event)=>checkRegex(event)},
onDone:{target:"end"}
onError:{target:"state1"}
},
end: {
type: "final"
}
}
});组件使用情况:
const [current,send] = useMachine(regexMachine);
send("VALIDATE","aaabbbcc");https://stackoverflow.com/questions/60641730
复制相似问题