使用正则表达式创建HTML标记

Creating HTML tags using regex

本文关键字:HTML 标记 创建 正则表达式      更新时间:2023-09-26

我正在寻找一个javascript函数,将能够转:

- point one
- point two
- point three
为HTML

:

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

任何帮助都将非常感激!

假设您的输入是字符串(例如"- point one'n- point two..."),并且您希望您的输出为字符串:

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

您可以通过删除不需要的位并插入适当的标记来将其转换为HTML字符串:

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>');