JQuery how to insert tag <br/"> after each semicolon
I drop the CSS in the div and I am looking for it to format so that it is more legible. Basically what I want to do is insert a break tag after each semicolon. I searched around for a while, but I canβt find something that is consistent with what I'm trying to do.
I have something like this ...
HTML
<div class='test'> color:red;background-color:black; </div>β JQuery
var test = $('.test').text(); var result = test.match(/;/g); alert(result);β And I tried ..
var test = $('.test').text(); var result = test.match(/;/g); result.each(function(){ $('<br/>').insertAfter(';'); }); alert(result);β I also started a fiddle here .. Which basically just returns a matching character ... http://jsfiddle.net/krishollenbeck/zW3mj/9/ This is the only part I've managed to get to so far.
I feel like I'm going on the right track with this, but I know this is wrong because he is wrong. I think there is a way to insert a break tag after each matched element, but I'm not quite sure how to get there. Any help is much appreciated. Thanks...
You can use the regular javascript .replace() method as follows:
β$(document)β.ready(function(){ $(".test").html($(".test").html().replace(/;/g, ";<br />")); });β