Regex recipes

I’ve always been terrible at regex—here are some snippets that come in handy.

Get URLs from the src attribute of an HTML image tag

const srcRegex = /<img.*?src=['"](.*?)['"]/
const src = srcRegex.exec(string)[1]

// string = "<img src='' />"
// src = ""

Test whether a URL is internal or not

const internal = /^\/(?!\/)/.test(url)

Strip HTML tags out of a string

const stripHTML = (string) => string.replace(/<[^>]+>/g, '')