a REGULAR EXPRESSION is a thing that matches strings.
this language uses a REGULAR EXPRESSION to match "pasta" but it's done with a REGULAR EXPRESSION.
TRANSLATED:
make a term that accepts a REGULAR EXPRESSION source as its argument. when storing the REGULAR EXPRESSION, we need to add a "^" to the front of the source so that it matches from the BEGINNING of the input text.
class RegExpTerm extends Term {
constructor(regex) {
super()
this.source = regex.source
this.regex = new RegExp("^" + regex.source)
}
}
give it a name.
class RegExpTerm extends Term {
//...
name() {
return `/${this.source}/`
}
}
check is easy.
class RegExpTerm extends Term {
//...
check(text) {
return this.regex.test(text)
}
}
bite is easy too.
class RegExpTerm extends Term {
//...
bite(text) {
return text.match(this.regex)?.[0] ?? null
}
}
that's it.
back to the dream