用于插入span标签的正则表达式

时间:2017-08-14 15:06:59

标签: javascript html regex string

我需要一个正则表达式来将字符串替换为下面的包装格式。

示例输入:

C# Regular expression for combination of space, alpha and special-character. Also for delta

输出:

<span>C# <span>Regular <span>expression <span>for <span>combination <span>of </span><span>space<span>,</span> </span><span>alpha </span><span>and </span><span>special</span><span>-</span><span>character</span>. </span><span>Also </span><span>for </span><span>delta</span>

1 个答案:

答案 0 :(得分:2)

您可以使用此replace来电:

var txt = "C# Regular expression for combination of space, alpha and special-character. Also for delta";

var result = txt.replace(/[\w#]+\s*|[^\w#]+\s*/g, "<span>$&</span>\n");

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }