I am trying to create a tag editor like Stack Overflow.
I get this error indicated in the header if I actually cannot find the asterisk AND http: // containing the phrase in the text box. If I find only an asterisk containing the phrase, the error applies to this line:if(linkify.length!==0)
Here's jsfiddle to show what I mean.
Here is my HTML:
<textarea></textarea>
<div></div><button type="button">Markdownify</button>
Here is my JS:
var val=$('textarea').val(), boldify = val.match(/\*\*[A-Za-z0-9]+\*\*/gim),
italicify = val.match(/\*[A-Za-z0-9]+\*/gim),linkify = val.match(/http\:\/\/[A-z0-9]+\.[A-z0-9]+/gim);
$(document.body).on('click', 'button', function() {
val=$('textarea').val(), boldify = val.match(/\*\*[A-Za-z0-9]+\*\*/gim),
italicify = val.match(/\*[A-Za-z0-9]+\*/gim),linkify = val.match(/http\:\/\/[A-z0-9]+\.[A-z0-9]+/gim);
if (boldify.length!== 0){
for (var i=0; i < boldify.length; i++) {
var boldMatch= boldify[i],
boldReplace = boldMatch.replace(/\*\*[A-z0-9]+\*\*/gim, '<span style="font-weight:bold;color:blue;">'+boldMatch+'</span>'),
val = val.replace(boldMatch, boldReplace);
}
val = val.replace(/\*\*/gi, "");
}
if(italicify.length!== 0){
for(var j=0; j < italicify.length; j++){
var italicMatch= italicify[j],
italicReplace = italicMatch.replace(/\*[A-z0-9]+\*/gim, '<span style="font-style:italic;color:red;">'+italicMatch+'</span>');
val = val.replace(italicMatch, italicReplace);
}
val = val.replace(/\*/gi, "");
}
if(linkify.length!== 0){
for(var k=0; k < linkify.length; k++){
var linkMatch= linkify[k],
linkReplace = linkMatch.replace(/http:\/\/[A-z0-9]+\.[A-z0-9]+/gim, '<a href="'+linkMatch+'">'+linkMatch+'</a>');
val = val.replace(linkMatch, linkReplace);
}
}
$('div').html(val);
});
Any help would be greatly appreciated.
source
share