[英]Changing the tags with the text between the tags
我的標題聽起來很復雜,所以我將通過舉例來簡化它
我有這些自制標簽,例如
{tag}tagtext{tag}
我正在嘗試將其更改為
<span>same tagtext</span>
我嘗試使用正則表達式,但我無法真正弄清楚如何獲取標簽之間的文本,然后將標簽更改為其他內容。
我得到了標簽之間的文字
string.match('{tag}(.*){tag}');
同樣,所有標簽都是相同的,由於某種原因,match函數只給了我找到的第一個結果。 我想這與我的正則表達式有關。
我也不想做一個循環,因為它將開始作為實時編輯器工作,我需要保持性能快速。
/(\{tag\})(.*?)\1/g
^^^^^^^^^ ^^ ^^ ^
1 2 3 4
1: Match tag and put in group 1
2: *? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)
3: matches the same text as most recently matched by the 1st capturing group
4: Global pattern flags, all matches.
var temp = "{tag}tagtext1{tag}{tag}tagtext2{tag}\\n{tag}tagtext3{tag}{tag}tagtext4{tag}"; console.log(temp.replace(/(\\{tag\\})(.*?)\\1/g,"<span>$2</span>"));
這是您期望的結果嗎?
<span>tagtext1</span><span>tagtext2</span>
<span>tagtext3</span><span>tagtext4</span>
您必須在RegEx表達式后使用global修飾符或g才能啟用所有匹配項。 您正在使用哪種語言進行RegEx?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.