user5711656
user5711656

Reputation: 3678

How to split string in JavaScript with splitter?

can we split the input string with splitter /* ! */

I have string

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'}"

I am trying to split string using commented code /* ! */

Expected output

{
  "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';
  }"


}

or store in array only value.

Upvotes: 1

Views: 120

Answers (2)

Wyck
Wyck

Reputation: 11730

Here's one way using a regex to split it into an array based on the /*! ... */ structure.

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);

Upvotes: 2

Amadou Beye
Amadou Beye

Reputation: 2808

Assuming the sequence *//*! will always be a part of your string(just before the beginning of your expected result):

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);

Upvotes: 0

Related Questions