如何使用拆分器拆分javascript中的字符串?

z2acfund  于 2021-09-23  发布在  Java
关注(0)|答案(2)|浏览(290)

我们可以用拆分器拆分输入字符串吗 /* ! */ 我有绳子

var a ="/*! ###################################################### # Test.cs #  RELEASE: 1.1.1 # BUILD DATE: Fri Oct 30 2020 15:25:57 GMT-0700 (PDT) # COPYRIGHT ###################################################### *//*! MISC + SASS */a:focus,button:focus,div[tabindex]:focus,li[tabindex]:focus,span[tabindex]:focus{outline-offset:2px;outline-width:2px!important;outline-style:dotted!important;outline-color:currentColor}/*! GENERIC - BGIMG */.bgimg{background-size:cover;background-position:50% 50%;background-repeat:no-repeat}/*! CG2111 - test */span{color:red}/*! CG2112 - test */span{color:red}/*! INFO */#id{content:'1.1.1'}"

我正在尝试使用注解代码拆分字符串 /* ! */ 预期产量

{
  "MISC + SASS " : "span[tabindex]:focus,
  li[tabindex]:focus,
  div[tabindex]:focus,
  button:focus,
  a:focus {
    outline-offset: 2px;
    outline-width: 2px !important;
    outline-style: dotted !important;
    outline-color: currentColor;
  }",
  "GENERIC - BGIMG":".bgimg {
    background-size: cover;
    background-position: 50% 50%;
    background-repeat: no-repeat;
  }",
  "CG2111 - test":"span {
    color: red;
  }",
  "CG2112 - test":"span {
    color: red;
  }",
  "INFO":"#id {
    content: '1.1.1';
  }"

}

或仅存储在数组中的值。

eqfvzcg8

eqfvzcg81#

下面是一种使用正则表达式根据 /*! ... */ 结构。

const a = "/*! ###################################################### # Test.cs #  RELEASE: 1.1.1 # BUILD DATE: Fri Oct 30 2020 15:25:57 GMT-0700 (PDT) # COPYRIGHT ###################################################### *//*! MISC + SASS */a:focus,button:focus,div[tabindex]:focus,li[tabindex]:focus,span[tabindex]:focus{outline-offset:2px;outline-width:2px!important;outline-style:dotted!important;outline-color:currentColor}/*! GENERIC - BGIMG */.bgimg{background-size:cover;background-position:50% 50%;background-repeat:no-repeat}/*! CG2111 - test */span{color:red}/*! CG2112 - test */span{color:red}/*! INFO */#id{content:'1.1.1'}";

const result = {}, temp = a.split(/\/\*\!\s*(.*?)\s*\*\//).slice(1);
while (temp.length) result[temp.shift()] = temp.shift();

console.log(result);
ssm49v7z

ssm49v7z2#

假设序列 *//*! 将始终是字符串的一部分(就在预期结果开始之前):

const a ="/*! ###################################################### # Test.cs #  RELEASE: 1.1.1 # BUILD DATE: Fri Oct 30 2020 15:25:57 GMT-0700 (PDT) # COPYRIGHT ###################################################### *//*! MISC + SASS */a:focus,button:focus,div[tabindex]:focus,li[tabindex]:focus,span[tabindex]:focus{outline-offset:2px;outline-width:2px!important;outline-style:dotted!important;outline-color:currentColor}/*! GENERIC - BGIMG */.bgimg{background-size:cover;background-position:50% 50%;background-repeat:no-repeat}/*! CG2111 - test */span{color:red}/*! CG2112 - test */span{color:red}/*! INFO */#id{content:'1.1.1'}";

const arr = a.split('*//*!');
const arr2 = arr[1].split('/*!');

const obj = {};
for(const item of arr2){
  const tmp = item.split('*/');
  obj[tmp[0]] = tmp[1];
}
console.log(obj);

相关问题