user884899
user884899

Reputation: 81

Creating HTML tags using regex

I am looking for a javascript function that will be able to turn:

- point one
- point two
- point three

into HTML:

<li>point one</li><li>point two</li><li>point three</li>

Any help would be greatly appreciated!

Upvotes: 0

Views: 75

Answers (2)

RobG
RobG

Reputation: 147453

You can convert it to an HTML string by removing the unwanted bits an inserting appropriate tags:

var s = "- point one\n- point two\n- point three"

// <li>point one<li>point two<li>point three
var html = '<li>' + s.replace(/- /g,'').split('\n').join('<li>');

Upvotes: 1

maerics
maerics

Reputation: 156534

Assuming your input is a string (e.g. "- point one\n- point two...") and you want your output as a string:

function convertListItemsToLiTags(s) {
  return (""+s).replace(/^-\s*(.*?)\s*$/gm, function(s, m1) {
    return "<li>" + m1 + "</li>";
  });
}

Upvotes: 1

Related Questions