the HTML/concept:
<textarea id="input"></textarea>
<button onclick="format()">submit</button>
<textarea id="output"></textarea>
I regularly have to convert docs into html for clients, and I'm tired of having to find/replace + manually change-to/add the appropriate HTML. So I looked for my dream formatter, but couldn't find anything (please post if you know about one that fits), so I figured I'd just wright my own with javascript. It's very straight forward but I'm unfamiliar with regular expressions and having some trouble, here's what I've been able to piece together using regexp I've found in other posts:
var email = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
var url = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var br = /(\r\n|\n|\r)/gm;
function format() {
var input = $('#input').val();
var check1 = input.replace(br,"<br>");
var check2 = check1.replace(url,'<a href="$1" target="blank">$1</a>');
var check3 = check2.replace(email, '<a href="mailto:$1">$1</a>' );
var check4 = check3.replace(etc, ...);
var output = check4;
$('#output').val(output);
}
There's a couple more things I want to do, but can't seem to find/write the correct regexp, these are:
- find any bold characters, and replace them with appropriate html/css
- find any italic characters, and replace them with appropriate html/css
find particular characters (©,“,”,ñ,etc) and replace them with the appropriate characters/entities ie:
[© , " , " , ñ , etc]
My apologies if this has been answered, but I can't seem to find these bits (perhaps I'm asking the wrong questions?), any help finding bold/italic as well as replacing specific characters/entities would be great! Also, if I'm going about this the wrong way please call me out. Thans so much!