Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7129957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:17:55+00:00 2026-05-28T11:17:55+00:00

I have the following text as a JavaScript string .mybox { display: block; width:

  • 0

I have the following text as a JavaScript string

.mybox {
 display: block; 
 width: 20px; 
 height: 20px;
 background-color: rgb(204, 204, 204);
 }

I want to convert to a JavaScript Object

var mybox = {
 'display': 'block',
 'width': '20px',
 'height': '20px';
 'background-color': 'rgb(204, 204, 204)';
};

Any ideas or already made scripts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:17:55+00:00Added an answer on May 28, 2026 at 11:17 am

    This is the beginning of a parser that may do what you want. Of course it needs work, especially if you want to handle any generic css that may be provided. This assumes that input css is written as you provided, with the first row being the name of the property, the last row being a ‘}’ and so on.

    If you don’t want to handle only basic properties, writing a complex parser is not an easy task. For example, what if you declare something like:

    input[type="text"],
    table > tr:nth-child(2),
    #link a:hover {
        -webkit-transition: width 2s; /* Safari and Chrome */
    }
    

    This is valid css, but how would you extract a valid javascript variable name from it? How to convert -webkit-transition into a meaningful property name? The whole task smells like you’re doing it all wrong. Instead of working on a parser, I’d work on a more stable solution at all.

    By the way, here is the code you may start from:

        var s = '.mybox {\n';
        s += 'display: block;\n';
        s += 'width: 20px;\n';
        s += 'height: 20px;\n';
        s += 'background-color: rgb(204, 204, 204);\n';
        s += '}\n';
    
        // split css by line
        var css_rows = s.split('\n'); 
    
        // filter out empty elements and strip ';'      
        css_rows = css_rows.filter(function(x){ return x != '' }).map(function(x){ return x.trim().replace(';', '') });
    
        // create object
        var json_name = css_rows[0].trim().replace(/[\.\{\ \#]/g, '');
        eval('var ' + json_name + ' = {};');
        // remove first and last element
        css_rows = css_rows.splice(1, css_rows.length-2)
    
        for (elem in css_rows)
        {
            var elem_parts = css_rows[elem].split(':');
            var property_name = elem_parts[0].trim().replace('-', '');
            var property_value = elem_parts[1].trim();
            eval(json_name + '.' + property_name + ' = "' + property_value + '";');
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following javascript: css = document.createElement('style'); css.setAttribute('type', 'text/css'); css_data = document.createTextNode(''); css.appendChild(css_data);
I have the following code in website1.com: <script type=text/javascript> document.cookie = qwe=1; alert(document.cookie); </script>
I have the following code: <html><body> <script type=text/javascript> function date1(){ ???? alert(<%=d%>);}</script> <input type=button
I have following jQuery/JS code <script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js></script> <script> $.getJSON(http://example/topics, {}, function(data){ console.log( data
I have the following very simple Javascript-compatible regular expression: <script type=text/javascript id=(.+) src=([^]+)> I
I have the following jQuery function embedded in an MVC Razor page: <script type=text/javascript>
In JavaScript, I have the following: function replaceURLWithHTMLLinks(text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; return
I have following page <html> <head> <script type=text/javascript src=e01.js></script> </head> <body> <script type=text/javascript> var
I have following text in a file 23456789 When I tried to replace the
I have the following text tooooooooooooon According to this book I'm reading, when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.