Small lib to find all links in a string and get their positions or wrap them with something(like html
<a>
tag)
npm install --save links-finder
or
yarn add links-finder
or
Download our minified version
or
Download our gziped version
Take a look here
import linksFinder from 'links-finder';
console.log(linksFinder.findLinks('magic http://blah.com another')); // will log [{start: 6, end: 20}]
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}]
import linksFinder from 'links-finder';
console.log(linksFinder.findLinks('magic')); // will log []
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}]
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>'
import linksFinder from 'links-finder';
console.log(linksFinder.wrapLinks('magic http://blah.com', {
onMatch: (link) => ''
})); // will log 'magic '
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
Feel free to open ISSUES and create pull requests