C#taghelper字符串替换

时间:2016-04-13 19:36:19

标签: c# regex asp.net-core-mvc

我正在开发一个.net核心标记器,它将简单的标记转换为我的移动和桌面菜单所需的标记,我需要一些帮助/复习一下,完成字符串操作的最佳方法是因为我不幸生锈了。起始标记示例如下。

<topnav>
    <a asp-controller="Home" asp-action="Index">Home</a>
    <a asp-controller="Home" asp-action="Test1">Test1</a>
    <a asp-controller="Home" asp-action="Test2">Test2</a>
    <div>
        <span>Nested Section</span>
        <a asp-controller="Home" asp-action="Test3">Test3</a>
        <a asp-controller="Home" asp-action="Test4">Test4</a>
    </div>
</topnav>

我想在确定......

的同时复制这种结构
<span>Nested Section</span>

和...

<a asp-controller="Home" asp-action="Test3">Test3</a>
<a asp-controller="Home" asp-action="Test4">Test4</a>

将插入两个不同的模板中以创建...

<a asp-controller="Home" asp-action="Index">Home</a>
<a asp-controller="Home" asp-action="Test1">Test1</a>
<a asp-controller="Home" asp-action="Test2">Test2</a>
<div class='accordion'>
    <div class='title'>
        <span>Nested Section</span>
        <i class='icon'></i>
    </div>
    <div class='content'>
        <a asp-controller="Home" asp-action="Test3">Test3</a>
        <a asp-controller="Home" asp-action="Test4">Test4</a>
    </div>
</div>

和...

<a asp-controller="Home" asp-action="Index">Home</a>
<a asp-controller="Home" asp-action="Test1">Test1</a>
<a asp-controller="Home" asp-action="Test2">Test2</a>
<div class='dropdown'>
    <span>Nested Section</span>
    <i class='icon'></i>
    <div class='menu'>
        <a asp-controller="Home" asp-action="Test3">Test3</a>
        <a asp-controller="Home" asp-action="Test4">Test4</a>
    </div>
</div>
然后将

插入到第三个模板中以创建...

<nav class='sidebar menu'>
    <a asp-controller="Home" asp-action="Index">Home</a>
    <a asp-controller="Home" asp-action="Test1">Test1</a>
    <a asp-controller="Home" asp-action="Test2">Test2</a>
    <div class='accordion'>
        <div class='title'>
            <span>Nested Section</span>
            <i class='icon'></i>
        </div>
        <div class='content'>
            <a asp-controller="Home" asp-action="Test3">Test3</a>
            <a asp-controller="Home" asp-action="Test4">Test4</a>
        </div>
    </div>
</nav>

<section class='top menu'>
    <nav class='computer'>
        <a href="#"><img alt="logo"/></a>
        <a asp-controller="Home" asp-action="Index">Home</a>
        <a asp-controller="Home" asp-action="Test1">Test1</a>
        <a asp-controller="Home" asp-action="Test2">Test2</a>
        <div class='dropdown'>
            <span>Nested Section</span>
            <i class='icon'></i>
            <div class='menu'>
                <a asp-controller="Home" asp-action="Test3">Test3</a>
                <a asp-controller="Home" asp-action="Test4">Test4</a>
            </div>
        </div>
    </nav>
    <div class='mobile'>
        <a href="#"><img alt="logo"/></a>
        <a href='#'><i class='sidebar icon'></i></a>
    </div>
</section>

我已经构建了模板,只需要从原始topnav标记的内容中完成字符串解析并适当插入。我还希望该解决方案能够处理需要多个嵌套的菜单,例如......

<topnav>
    <a asp-controller="Home" asp-action="Index">Home</a>
    <a asp-controller="Home" asp-action="Test1">Test1</a>
    <a asp-controller="Home" asp-action="Test2">Test2</a>
    <div>
        <span>Nested Section</span>
        <a asp-controller="Home" asp-action="Test3">Test3</a>
        <div>
            <span>Nested Section 2</span>
            <a asp-controller="Home" asp-action="Test5">Test5</a>
            <a asp-controller="Home" asp-action="Test6">Test6</a>
        </div>
        <a asp-controller="Home" asp-action="Test4">Test4</a>
    </div>
</topnav>

我认为正则表达式是要走的路,但我想在走向错误的方向之前收集一些意见。如果需要,也可以添加到原始标记,但我要求它尽可能地小。请提供示例!

谢谢!

0 个答案:

没有答案