Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Created July 4, 2019 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseluisq/6aad13c902758c7eb72d629b16e0bbca to your computer and use it in GitHub Desktop.
Save joseluisq/6aad13c902758c7eb72d629b16e0bbca to your computer and use it in GitHub Desktop.
Replace non-www to www string url in Javascript
const REGEX_WWW = /^https:\/\/(?!www\.)(.*)$/

"https://domain.com".replace(REGEX_WWW, "https://www.$1")
// "https://www.domain.com"

"https://www.domain.com".replace(REGEX_WWW, "https://www.$1")
// "https://www.domain.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment