Skip to content

Instantly share code, notes, and snippets.

@bogdanpetru
Created September 26, 2016 13:59
Show Gist options
  • Save bogdanpetru/6b56d366bd8914e9485d9b137916ca3a to your computer and use it in GitHub Desktop.
Save bogdanpetru/6b56d366bd8914e9485d9b137916ca3a to your computer and use it in GitHub Desktop.
Parses text with anchor like markdown text
var re = /\[(.+?)]\((.+?)\)/ig;
var str = '[Mihaela Popescu](popover://contact/891) vezi [CE1211](popover://ask/21)';
var m;
var foundMatches = []
var result = [str]
while ((m = re.exec(str)) !== null) {
// every m looks like this
// ["[Mihaela Popescu](popover://contact/891)", "Mihaela Popescu", "popover://contact/891"]
foundMatches.push(m)
}
// replace every part
foundMatches.forEach((match) => {
result = result.map((substring) => {
if (typeof substring === 'string') {
let localResult = []
substring.split(match[0]).forEach((part, index, list) => {
if (index !== 0 && index !== (list.length - 1)){
localResult.push(<a href={match[2]}>{match[1]}</a>)
}
})
} else {
return substring
}
})
})
// cumva trebuie sa reiterezi peste result de fiecare data cand gasesti ceva
{
title: 'bla bla',
url: '/blabla',
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment