[英]Creating HTML tags using regex
I am looking for a javascript function that will be able to turn: 我正在寻找一个能够转向的javascript函数:
- point one
- point two
- point three
into HTML: 到HTML:
<li>point one</li><li>point two</li><li>point three</li>
Any help would be greatly appreciated! 任何帮助将不胜感激!
Assuming your input is a string (eg "- point one\\n- point two..."
) and you want your output as a string: 假设您的输入是一个字符串(例如
"- point one\\n- point two..."
)并且您希望输出为字符串:
function convertListItemsToLiTags(s) {
return (""+s).replace(/^-\s*(.*?)\s*$/gm, function(s, m1) {
return "<li>" + m1 + "</li>";
});
}
You can convert it to an HTML string by removing the unwanted bits an inserting appropriate tags: 您可以通过删除插入适当标记的不需要的位将其转换为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>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.