Sin*_*our 211 javascript regex
我有一个字符串,如hello _there_.我想,以取代两个下划线<div>并</div>分别使用JavaScript的.输出(因此)看起来像hello <div>there</div>.该字符串可能包含多对下划线.
我所寻找的是一个方法来无论是运行在每场比赛,红宝石做它的方式的函数:
"hello _there_".gsub(/_.*?_/) { |m| "<div>" + m[1..-2] + "</div>" }
或者能够引用匹配的组,再次以红宝石的方式完成:
"hello _there_".gsub(/_(.*?)_/, "<div>\\1</div>")
任何想法或建议?
air*_*tyh 350
"hello _there_".replace(/_(.*?)_/, function(a, b){
    return '<div>' + b + '</div>';
})
哦,或者你也可以:
"hello _there_".replace(/_(.*?)_/, "<div>$1</div>")
Eif*_*ion 32
你可以用replace而不是gsub.
"hello _there_".replace(/_(.*?)_/g, "<div>\$1</div>")
Moh*_*lal 15
对于由 指定的替换字符串和替换模式$。这里有一份简历:
链接到文档:这里
"hello _there_".replace(/_(.*?)_/g, "<div>$1</div>")
笔记: 
如果您想$在替换字符串中使用$$. 与 vscode 片段系统相同。
| 归档时间: | 
 | 
| 查看次数: | 115733 次 | 
| 最近记录: |