7

I am using the hyperref package and I want to make a command for writing \hrefs once with their protocol removed. For example,

  • https://google.com becomes linked google.com
  • http://google.com becomes linked google.com

normally to do this I could write

\documentclass{article}
\usepackage{hyperref}

\begin{document}
\href{https://google.com}{google.com}
\end{document}

however, it would be ideal if I could create a command that works like so

\newcommand{\uri}[1]{
  \href{#1}{
    % somehow remove http[s]?:\/\/ from #1
  }
}

1 Answer 1

8

With no special characters in the URL, it's quite easy:

\documentclass{article}
\usepackage{hyperref}

\newcommand{\rhref}[1]{%
  \href{#1}{\stripprotocol#1\stripprotocol}%
}
\def\stripprotocol#1//#2\stripprotocol{#2}

\begin{document}

\rhref{http://google.com}

\rhref{https://tex.stackexchange.com}

\end{document}

If you insist on using l3regex:

\documentclass{article}
\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn
\NewDocumentCommand{\rhref}{m}
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  \regex_replace_once:nnN { \A .*? // } { } \l_tmpa_tl
  \href{#1}{\l_tmpa_tl}
}
\ExplSyntaxOff

\begin{document}

\rhref{http://google.com}

\rhref{https://tex.stackexchange.com}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .