簡體   English   中英

使用正則表達式創建HTML標記

[英]Creating HTML tags using regex

我正在尋找一個能夠轉向的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>');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM