JSFiddle - React, Tailwind, and code Playground

by f0t0n

JavaScript

function Matcher(pattern, flags) {
    this.setExpression(pattern, flags);
}

Matcher.prototype.setExpression = function(pattern, flags) {
    this.pattern = pattern;
    this.flags = flags;
    this.re = new RegExp(this.pattern, this.flags);
};

Matcher.prototype.append = function(pattern) {
    this.setExpression(this.pattern + pattern, this.flags);
};

Matcher.prototype.test = function(str) {
    return this.re.test(str);
};

var matcher = new Matcher('j.*s.*', 'i'),
    str = 'JavaScript';

function test() {
    console.log(matcher.re.source, ':', matcher.test(str));
}

test(); // true
matcher.append('ri');
test(); // true
matcher.append('.t');
test(); // true
matcher.append('whatever');
test(); // false