Giter VIP home page Giter VIP logo

links-finder's Introduction

links-finder

Small lib to find all links in a string and get their positions or wrap them with something(like html <a> tag)

Install library

  npm install --save links-finder

or

  yarn add links-finder

or

Download our minified version

or

Download our gziped version


Live demo

Take a look here


Examples of finding links

Default usage
  import linksFinder from 'links-finder';
  console.log(linksFinder.findLinks('magic http://blah.com another')); // will log [{start: 6, end: 20}]
Several links
  import linksFinder from 'links-finder';
  console.log(linksFinder.findLinks('magic http://blah.com and https://x.com')); // will log [{start: 6, end: 20}, {start: 26, end: 38}]
No links
  import linksFinder from 'links-finder';
  console.log(linksFinder.findLinks('magic')); // will log []
Links without http
  import linksFinder from 'links-finder';
  console.log(linksFinder.findLinks('magic www.blah.com and some')); // will log [{start: 6, end: 17}]
  console.log(linksFinder.findLinks('magic blah.com')); // will log [{start: 6, end: 13}]

Examples of wrapping links

Example of wrapping link with in React way
  import linksFinder from 'links-finder';
  console.log(linksFinder.wrapLinks('magic http://blah.com', {
    onMatch: (link) => <a href={link}>{link}</a>
  })); // will log 'magic <a href="http://blah.com"/>http://blah.com</a>'
Example of removing all links
  import linksFinder from 'links-finder';
  console.log(linksFinder.wrapLinks('magic http://blah.com', {
    onMatch: (link) => ''
  })); // will log 'magic '

Examples with tree shaking

  import { findLinks } from 'links-finder';
  console.log(findLinks('magic http://blah.com')); // will log [{start: 6, end: 20}]
  import { wrapLinks } from 'links-finder';
  console.log(wrapLinks('magic http://blah.com', {
    onMatch: (link) => <a href={link}>{link}</a>
  })); // will log 'magic <a href="http://blah.com"/>http://blah.com</a>'
For more example, you can take a looks to our test spec file

This lib is super fast and super tiny(< 6kb)
We support all modern browsers starting from IE9

Feel free to open ISSUES and create pull requests

links-finder's People

Contributors

faradey27 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.