user3444873
user3444873

Reputation: 53

Regex for replacing mailto links (Javascript)

I want to write a Javascript function to replace href tags that contain a mailto link.

<a href="mailto:[email protected]">Email me</a>!

Should become:

<a href="&lt;%='mailto:[email protected]'%&gt;">Email me</a>!

This is for an application that feeds HTML into a third-party service, and the service is broken in that it won't accept mailto URLs, but if we mask it as a variable, it will accept it. I'm just having a hard time wrapping my non-regex-friendly brain around only replacing the inner part of the href tags.

Thanks so much for your help!

Upvotes: 0

Views: 793

Answers (1)

le_m
le_m

Reputation: 20238

You can't fully parse HTML with RegEx, but you can use an HTML parser and just parse and replace the anchor tag's href attributes with regex.

The following specifications are relevant:

The href attribute on a and area elements must have a value that is a valid URL potentially surrounded by spaces. ...

A string is a valid URL potentially surrounded by spaces if, after stripping leading and trailing white space from it, it is a valid URL. ...

A URL is a valid URL if it conforms to the authoring conformance requirements in the WHATWG URL specification. [URL]

See http://w3c.github.io/html/single-page.html#valid-url

An absolute-URL string must be [...] a URL-scheme string that is an ASCII case-insensitive match for a special scheme and not an ASCII case-insensitive match for "file", followed by U+003A (:) and a scheme-relative-special-URL string ...

See https://url.spec.what

A robust regex thus has to be case insensitive and accept whitespace:

let href = " MailTO:[email protected]  ";
let result = href.replace(/^\s*mailto:.*$/i, "&lt;%='$&'%&gt;");
console.log(result);

While you can also parse anchor element attributes with regex, you need an HTML parser to find the anchor elements within your HTML documents. And since you need an HTML parser anyway, you can rely on it to extract the anchor elements and their href attributes, too.

Upvotes: 2

Related Questions