Skip to content

Instantly share code, notes, and snippets.

Created January 25, 2017 20:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Create a regex from parts using a tagged template.
function regexSupplant (strings, ...values) {
let parts = []
while (strings.length || values.length) {
if (strings.length) {
.map((val) => val.trim())
if (values.length) {
.replace(/(^\/)|(\/$)/g, '')
const regexpish = parts.join('')
const pattern = regexpish.replace(/^\/|\/\w*$/g, '')
const flags = (/\/(\w*)$/g).exec(regexpish)[1]
return new RegExp(pattern, flags)
const validMentionPrecedingChars = /(?:^|[^a-zA-Z0-9_!#$%&*@@]|(?:^|[^a-zA-Z0-9_+~.-])(?:rt|RT|rT|Rt):?)/;
const atSigns = /[@@]/;
const screenName = /[a-zA-Z0-9_]{1,20}/;
const list = /\/[a-zA-Z][a-zA-Z0-9_\-]{0,24}/;
const validMentionOrList = regexSupplant`/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment