Created
September 26, 2016 13:59
-
-
Save bogdanpetru/6b56d366bd8914e9485d9b137916ca3a to your computer and use it in GitHub Desktop.
Parses text with anchor like markdown text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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