Jump To

Linkify String Interface

Use linkify-string to replace links in plain-text strings with anchor tags.

This function will not parse strings with HTML. Use one of the following instead, depending on your application:

Installation

Node.js module

Install from the command line with NPM

npm install linkifyjs linkify-string

Import into your JavaScript with require

const linkifyStr = require("linkify-string");

or with ES modules

import linkifyStr from "linkify-string";

Browser globals

Download linkify and extract the contents into your website’s assets directory. Include the following scripts in your HTML:

<script src="linkify.js"></script>
<script src="linkify-string.js"></script>

Usage

const options = {
  /* … */
};
const str = "For help with GitHub.com, please email support@github.com";
linkifyStr(str, options);
// or
str.linkify(options);

Returns

'For help with <a href="http://github.com" target="_blank">GitHub.com</a>, please email <a href="mailto:support@github.com">support@github.com</a>';

Usage with HTML

linkify-string automatically escapes HTML input.

const options = {
  /* … */
};
const str = "<p>For help with GitHub.com, please email support@github.com</p>";
linkifyStr(str, options);
// or
str.linkify(options);

Returns

'&lt;p&gt;For help with <a href="http://github.com">GitHub.com</a>, please email <a href="mailto:support@github.com">support@github.com</a>&lt;/p&gt;';

See Cross-Site Scripting for more about linkify and XSS.

Use linkify-html if you’d like to preserve all HTML entities.

Params

  • string str String to linkify
  • Object [options] Options object

Returns string Linkified string