JSFiddle - React, Tailwind, and code Playground

by brigand

JavaScript

class StartsWith {
	constructor(pattern) {
	  this.pattern = pattern;
  }
  [Symbol.match](string) {
	  return string.startsWith(this.pattern) ? [string] : null;
  }
  toString() {
	  return `StartsWith(${this.pattern})`;
  }
}

const checks = [
	/f/,
  /o/,
  /b/,
  new StartsWith('f'),
  new StartsWith('o'),
];


for (const check of checks) {
	console.log('Running check: ' + check);
	console.log('Result:', 'foo'.match(check));
}