0

I've build a very simple RTE which allows me to colorize my text and make it bold. I've worked with an Iframe and designmode.

What I now get is something like this:

this is my Text which is <strong>strong</strong> and <font color="#ff0000">colored</font> or <strong><font-color="#00ff00">both</font></strong>

Now I want to convert this HTML into an JavaScript object to transform it into an SVG with SnapSvg. For this I tried a lot of diffrent regular expressions but I've never worked with them much and the result is not what I need.

I was trying something like this:

function getMatches (text) {
    var regexp = /<[^>]*>/; 
    var matches = regexp.exec(text);
    if(matches !== null) {
        console.log(matches);
    }
}

Does anyone have a better regex or maybe an idea how get the object I need?

Pablo Christiano
  • 365
  • 3
  • 21
  • 1
    What do you expect as result of the string you provided in the example? – ibrahim mahrir Jan 16 '17 at 07:58
  • 1
    [Don't use regex to parse HTML.](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#1732454) Use an HTML parser, then work with the resulting DOM tree. What kind of "object" do you want? – T.J. Crowder Jan 16 '17 at 07:58
  • What i need is an object with properties like color, text and weight. I hoped for some indice to split the string so I could create an array of Objects with these attributes – Pablo Christiano Jan 16 '17 at 08:05
  • for example: [{bold: true, color: 'red', content:'H'}, {bold: true, color: 'red', content:'e'},{bold: true, color: 'red', content:'l'},{bold: true, color: 'red', content:'l'},{bold: false, color: 'black', content:'o'}] – Pablo Christiano Jan 16 '17 at 08:07
  • Use dom properties not regex to do it. Browsers already create objects (dom nodes) so iterate those nodes and map them to whatever it is you want – charlietfl Jan 16 '17 at 15:53

0 Answers0