【问题标题】:How to take particular character from a string and store it in a array如何从字符串中获取特定字符并将其存储在数组中
【发布时间】:2022-07-20 21:53:58
【问题描述】:

var element = "Connectorparent3cchild4d" element.match(/\d+/g)

之前编写的代码是从字符串中获取数值

但现在 var element = "Connectorparentabcchildcd"

有没有办法将值 abc 和 cd 存储在数组中

我需要将 Connectorparent 和 child 之后的值存储在 typescript 的数组中

【问题讨论】:

    标签: typescript


    【解决方案1】:

    您可以先删除“Connectorparent”并拆分“child”以在数组中同时获取 abccd

    var element =  "Connectorparent3cchild4d"
    const arr = element.replace("Connectorparent", "").split("child");
    console.log(arr);
    

    【讨论】:

      【解决方案2】:

      试试/Connectorparent(.*)child(.*)/ 作为你的正则表达式:

      var element = "Connectorparent3cchild4d"
      const [all, parrent, child] = element.match(/Connectorparent(.*)child(.*)/)
      

      您可以阅读有关正则表达式中的组以更好地理解,或使用https://regexr.com/ 来解释或构建您的正则表达式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-05
        • 1970-01-01
        • 2014-07-29
        • 2021-03-28
        • 2017-07-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多