简体   繁体   English

如何在 JavaScript 中将 CSS 代码转换为数组

[英]How can I convert CSS codes to array in JavaScript

For example, I want these codes例如,我想要这些代码

.a {color: red; background: black}
.b {color: green; background: white}
.c {color: red; border: silver}
.d {align: center; abc: silver}

to convert to an array like this转换为这样的数组

var css = [
    ".a {color: red; background: blck}",
    ".b {color: green; background: white}",
    ".c {color: red; border: silver}",
    ".d {align: center; abc: silver}"
];

Is it possible?是否可以? TIA TIA

There are several ways you can "parse" strings in Javascript.有几种方法可以在 Javascript 中“解析”字符串。 The most advanced would be to use a regular expression, but for simpler tasks you might instead want to use methods like substr and split .最先进的方法是使用正则表达式,但对于更简单的任务,您可能想要使用substrsplit等方法。

split will let you split a string up, so for instance: split将让您拆分一个字符串,例如:

".a {color: red; background: blck}".split('{')

would result in an array with two parts, the part before "{", and the part after:将导致一个包含两部分的数组,“{”之前的部分和之后的部分:

[".a ", "color: red; background: blck}"]

substr will instead let you cut off part of a string. substr会让你切断字符串的一部分。 For instance:例如:

".a {color: red; background: blck}".substr(4)

would skip the first four characters:将跳过前四个字符:

"color: red; background: blck}"

while:尽管:

".a {color: red; background: blck}".substr(4, 10)

would skip the first four, then use the next ten, then skip the rest:将跳过前四个,然后使用接下来的十个,然后跳过其余的:

"color: red"

You can read about these and other string prototype methods here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String您可以在此处阅读有关这些和其他字符串原型方法的信息: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

Try following JS:尝试以下JS:

 var cssStr = ".a {color: red; background: black}.b {color: green; background: white}.c {color: red; border: silver}.d {align: center; abc: silver}"; var css = cssStr.split('}').filter(u=> u!="").map(i => i + "}"); console.log(css);

Try this尝试这个

var string = `.a {color: red; background: black}.b {color: green; background: white}.c {color: red; border: silver}.d {align: center; abc: silver}`;
var css = [];
string.split('}').map(item => {
    var item2 = item.trim();
    var item3 = (item2 +"}");
    css.push(item3)
});
css.pop(); 
console.log(css);

Following JS would help,遵循 JS 会有所帮助,

      var cssStr = ".a {color: red; background: black}.b {color: green; background: white}.c {color: red; border: silver}.d {align: center; abc: silver}";

      //Using '.'
      var specialCharacter = "."; 
      var css = cssStr.split(specialCharacter).map(i => specialCharacter + i).filter(i => i!=specialCharacter);

      //Using '}'
      var specialCharacter = "}"; 
      var css = cssStr.split(specialCharacter).map(i => i + specialCharacter).filter(i => i!=specialCharacter);

Having filter would avoid the empty or additional value in the final array.使用过滤器可以避免最终数组中的空值或附加值。

It seems clean using RegEx /\\s(?=\\.)/g :使用 RegEx /\\s(?=\\.)/g看起来很干净:

 var style = `.a {color: red; background: black} .b {color: green; background: white} .c {color: red; border: silver} .d {align: center; abc: silver}`; var res = style.trim().split(/\\s(?=\\.)/g); console.log(res);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM