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

  • SEARCH
  • Home
  • 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 985045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:06:30+00:00 2026-05-16T05:06:30+00:00

As an exercise in learning RegEx in JavaScript, I’ve been trying to select an

  • 0

As an exercise in learning RegEx in JavaScript, I’ve been trying to select an attribute’s value within a CSS stylesheet. Given the following CSS:

foo { 
  color: rgb(0, 0, 0)     !important;
  background-color: rgb(0, 0, 0) !important;
  border-left: 1px solid #111; 
}

and a function getStyle(css, attribute) such that getStyle(css, 'background-color') returns "rgb(0, 0, 0)", I’ve come up with the following code:

function getStyle(css, attribute) {
  var pattern = new RegExp(attribute + ':\\s+([^!;]+)');
  return pattern.exec(css);
}

This gets pretty close: it matches until it hits a ! or a ;, whichever comes first. But in the case of getStyle(css, 'color'), it also returns the whitespace after rgb(0, 0, 0) and before !important:

color = "rgb(0, 0, 0)      "
background-color = "rgb(0, 0, 0) "
border-left =  "1px solid #111"

I’ve tried things like attribute + ':\\s+([^\\s+!;]+)', but that winds up returning only rgb(0, because of the spaces in the value. I’ve also been able to get the expected output using different regex and look behinds, but JavaScript doesn’t support them.

I’ve also thought that maybe I could indicate something like match while \\s+! or \\s+.+; isn’t true: that is, stop matching once it finds whitespace before a specific character, but I’m not sure how to key that in regex.

I could just as easily strip the whitespace in a second step, but I’m wondering: is there a way to get the expected output in one RegEx?

  • 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-16T05:06:31+00:00Added an answer on May 16, 2026 at 5:06 am

    I would use

    '\\s*:\\s*([^\\s+!;]+(?:\\s+[^\\s+!;]+)*)'
    

    If there’s some whitespace that’s followed by more non-whitespace/bang/semicolon characters, treat it as a continuation of the attribute value.

    I also changed the first part, because the whitespace before and after the colon is optional.

    You might want to add something like '[\\s;]' + to the beginning, too. The way you have it, the color regex will match both the color and background-color attributes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to do something as a learning exercise that I thought should
I'm trying (as a self-learning exercise) to create a Clojure macro that will generate
I am trying to write a Spider Solitaire player as an exercise in learning
I've been playing around with boost threads today as a learning exercise, and I've
As a learning exercise, I have been looking at how automatic type conversion works
As a learning exercise, I'm trying to implement a class which will emulate the
I am in the process of learning cocos2d-android. I have been following a tutorial
Im quite new to generics and as a learning exercise Im trying to create
I have been making a matrix class (as a learning exercise) and I have
I'm brand new to MVC and, as a learning exercise, I'm trying to re-write

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.