【问题标题】:How to remove leading and trailing space in a tag? regex如何删除标签中的前导和尾随空格?正则表达式
【发布时间】:2021-10-12 13:57:48
【问题描述】:

我在内容中有自定义标签,并且有关键字标签,上面有对应的id号

const tags = ["<i>", "<c>", "<b1>", "<b2>", "<b3>"];
keyword tags e.g. "<key2>", "<key10>"

我必须删除前导和尾随空格,因为我需要按单词拆分。


这是我的示例内容:

let content = 
`<b1> <c> "The Modern Amphibians" </c> </b1>
Modern <b>amphibians </b> have a simplified <key2>anatomy </key2> compared to their ancestors due to <i>  paedomorphosis</i>. 
Caused by two evolutionary trends: <b2> miniaturization  </b2> and an unusually.
Don’t think that this term’s work will be <key23> a piece of cake </key23>`

预期的输出将是(删除前导和尾随空格)

let output = 
`<b1><c>"The Modern Amphibians"</c></b1>
Modern <b>amphibians</b> have a simplified <key2>anatomy</key2> compared to their ancestors due to <i>paedomorphosis</i>. 
Caused by two evolutionary trends: <b2>miniaturization</b2> and an unusually.
Don’t think that this term’s work will be <key23>a piece of cake</key23>`

我尝试制作自己的正则表达式,从 c 标签开始,但我不确定这是否正确,因为我只需要删除空格,但我的正则表达式包含标签。

const customRegex = \((<c>\s)|(\s<\/c>))\g.

有人可以帮忙。谢谢。

【问题讨论】:

    标签: javascript regex


    【解决方案1】:

    你可以使用正则表达式/\s*(&lt;.*?&gt;)\s*/g

    let content = `<b1> <c> "The Modern Amphibians" </c> </b1>
    Modern <b>amphibians </b> have a simplified <key2>anatomy </key2> compared to their ancestors due to <i>  paedomorphosis</i>. 
    Caused by two evolutionary trends: <b2> miniaturization  </b2> and an unusually.
    Don’t think that this term’s work will be <key23> a piece of cake </key23>`;
    
    const result = content.replace(/\s*(<.*?>)\s*/g, "$1");
    console.log(result);

    【讨论】:

    • 这会删除换行符吗?
    • 这意味着我们只取括号中字符串的一部分,不包括在这个正则表达式中传递的所有其他内容
    • 在正则表达式部分中,我使用() 进行了分组,所以它的作用是从匹配中获取该组并仅用@987654327 中的文本替换它@ 和 ).
    • 是的,\s* 将选择任何空白字符,例如 spaceTabnew Line
    • 哦,不。它不应该。我的糟糕,我忘了把它放在描述中。
    【解决方案2】:

    你可以试试(&lt;[^&lt;&gt;\/]+&gt;)\s+|\s+(&lt;\/[^&lt;&gt;]+&gt;)

    这样可以确保只删除开始标签(例如&lt;s&gt;)或结束标签(例如&lt;/s&gt;)之前的空格

    const regex = /(<[^<>\/]+>)\s+|\s+(<\/[^<>]+>)/g;
    
    const content = 
    `<b1> <c> "The Modern Amphibians" </c> </b1>
    Modern <b>amphibians </b> have a simplified <key2>anatomy </key2> compared to their ancestors due to <i>  paedomorphosis</i>. 
    Caused by two evolutionary trends: <b2> miniaturization  </b2> and an unusually.
    Don’t think that this term’s work will be <key23> a piece of cake </key23>`;
    
    console.log(content.replace(regex, '$1$2'));

    【讨论】:

      【解决方案3】:
      let output = content.replaceAll(/(\s)?(\<\/?\w+\>)(\s)?/g, '$2')
      

      这应该可以工作

      【讨论】:

      • 结束标签先生不起作用。
      • 抱歉,已编辑,立即尝试
      【解决方案4】:

      您的 RegEx 方法是正确的,这里有一个功能会有所帮助:$1..$9

      此功能允许您“捕获”您匹配的 RegExp 的特定部分并在替换时引用它:

      const regex = /(/w+)/s+(/w+)/;
      const string = "John Smith";
      
      string.replace(regex, "$2, $1)); //" Smith, John" 
      

      要在您的情况下使用它,我们需要一个匹配任何标签并将其替换为无空格版本的正则表达式:

      const regex = (<.+>)\s+(<.+>)/g;
      const string = "<b1> <c> abc </c> </b1>";
      
      string.replace(regex, "$1$2");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-20
        • 1970-01-01
        • 2015-11-15
        • 1970-01-01
        相关资源
        最近更新 更多