1

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.

3
  • You can split a string with String.prototype.split Commented Jul 9, 2021 at 16:33
  • split is supposed to return an array but your expected output is an object... Commented Jul 9, 2021 at 16:35
  • This is not string splitting. Splitting takes some string of the form axb, axbxc, axbxcxd... etc. where x is some arbitrary string sequence that is considered "glue", to be removed during splitting, leaving you with a list of the separate, constituent parts a, b, c, etc. What you're showing is something completely different, far more akin to tokenizing/parsing. So please update your post to explain what you're actually trying to do: what is the input? (what does it represent) and what is the output? (what does it represent). Commented Jul 9, 2021 at 16:40

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.