This page is outdated

You are reading documentation for Linkify v2. Browse the latest v3+ documentation.

Jump To

Linkify String Interface

linkify-string is an interface for replacing links with anchor tags within JavaScript strings.

Note that this function will not parse HTML strings properly - use linkify-html instead. Alternatively, if you’re using linkify with a DOM, use linkify-jquery or linkify-element

Installation

Node.js/Browserify

npm install linkifyjs
var linkifyStr = require('linkifyjs/string');

or with ES6 modules

import linkifyStr from 'linkifyjs/string';

AMD

<script src="linkify.amd.js"></script>
<script src="linkify-string.amd.js"></script>
<script>
  require(['linkify-string'], function (linkifyStr) {
    // …
  });
</script>

Browser globals

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

Usage

var options = {/* … */};
var 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.

var options = {/* … */};
var 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" class="linkified" target="_blank">GitHub.com</a>, please email <a href="mailto:support@github.com" class="linkified">support@github.com</a>&lt;/p&gt;'

See Caveats 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