Finding a CSS class selector that does not include .io, .w3, and similar classes

My code accurately identifies CSS selectors within a string as shown below:

let selectors = index.toString().match(/\.-?[_a-zA-Z]+[\w-]*(?=[^{}]*\{)/g);

This method successfully matches css selectors like u-background-color-navajo-white in declarations such as:

.u-background-color-navajo-white {
    background-color: NavajoWhite !important;
    background-color: var(--color-navajo-white) !important;
  }

However, the expression also picks up top-level domains mentioned within comments in the CSS file, for example .com, .io, .org,, etc.

If anyone has suggestions on how to modify the regex pattern to exclude these domain names?

Testing Repository

git clone https://github.com/superflycss/utilities-layout
npm i
sfc build
node duplicates.1.js

The output will be:

    ole@mki:~/SuperflyCSS/utilities-layout$ node duplicates.1.js 
    [ '.com',
    '.com',
    '.io',
    '.io',
    '.io',
    '.md',
    '.org',
    '.org',
    '.org',
    '.w3',
    '.w3',
    '.w3' ]

Support for CLI

The issue has now been resolved. I am planning to integrate this script into the @superflycss/cli in the upcoming period for those who require a postcss builder / duplicates checker.

Answer №1

I have made a revision to your regular expression:

/(?:^|[, ])\.-?[_a-zA-Z]+[\w-]*(?=[^{}]*\{)/gm

The modification simply adds a condition at the beginning:

It must start with 'start of line' OR one of the following characters: comma or space.

This adjustment should specifically target CSS classes.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigating through a PHP table, tracking your progress with a

I have encountered a problem that I need help with. I have been searching the internet for information on how to add a progress bar to my PHP code. Most solutions I found use the style method to display the progress percentage. In the image below, you can ...

What is the inner workings behind Facebook applications?

Let me explain my current situation: I am looking to retrieve a list of emails for the friends associated with a specific user. Here is the step-by-step process I envision: A div box will appear prompting the user to log in using their Facebook creden ...

Change the CSS dynamically

I'm curious about how to create a similar effect like the one on this page: On the left side, there is a panel that allows for adjusting the template. What I've noticed is that when I change the color, different CSS files are used (blue.css, pur ...

Error Reference: The variable "angular" has not been properly defined in line 1 of the app.js

WelcomePage.js angular.module("travel") .controller('LoginCtrl', function($scope, AuthService, $ionicPopup, $state) { $scope.user = { name: '', password: '' }; $scope.login = function() { AuthService.login ...

When I click the toggler button, the collapse menu fails to close

I created a responsive menu using Bootstrap 4.0.0. The collapsible menu works by opening when clicked, however, it does not close when clicked again. Here is the code: html: <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a href ...

Express layouts error - undefined variable

While using express-ejs-layouts, I encountered an error in the console log: >> 5| <title><%= title %></title> title is not defined Despite having the title element defined in my layout.ejs file: <title><%= ...

Format specific words or characters in a text input

In HTML, when I want to display strings in a Text Input or TextArea, I am looking for a way to have specific substrings render with a box around them. I envision these boxed substrings as being treated as a single entity, similar to how highlighting or tex ...

Is it possible to activate events on select tags even when they have the disabled attribute?

All the select options on this page have been disabled with an ID that ends in _test, and everything seems to be functioning properly. However, I would like to display a title when the selects are disabled and the mouse hovers over them. The issue arises ...

Is there a way to make a table row clickable? I tried finding solutions online, but none of them seemed to work for me

Having trouble opening a new page when tapping on a cell (TR) using Javascript. Despite trying various online tutorials, I still can't get it to work properly. Any assistance would be greatly appreciated. Thank you. Below is the code snippet: fun ...

What is the best way to delete information from a database with the help of Redux

Lately, I've been grappling with the task of integrating redux into my project. Successfully posting data to the database using redux and then retrieving it in react was a win, but now I find myself struggling with how to handle deleting data through ...

JQuery Mobile applies X to all divs with the specified class

I am currently working on a JQuery mobile website with an image slider on 2 different pages. In order to activate the sliders, I am using the following JavaScript code: $(function () { $("#slider").excoloSlider(); }); where '#slider' refers ...

Activate BootstrapValidator to dynamically enable or disable the submit button as you type

Is there a way to keep the submit button enabled while typing in BootstrapValidator? ...

Comparing the Calculation of CSS Selector Specificity: Class versus Elements [archived]

Closed. This question requires additional information for debugging purposes. It is not currently accepting answers. ...

Upon each reload, the session in NodeJS will reset and a fresh session will be generated

I've been experiencing the same error all day long. Every time I try to reload the page, a new session is created. By the way, I am new to NodeJS and my English may not be perfect. var express = require('express'); var session = require(&a ...

Substitute unscrupulous quotation marks within a vector in R

I've encountered a predicament with a corrupt CSV file that contains lengthy text fields encompassing both double quotes and commas. After conducting some cleanup, I now have tab-separated fields as an array of complete lines (each line is a value). ...

Is there a way to prevent the page's navbar from getting hidden under the slide animation? I want to display the navbar separately at the top, with animations working as usual

I'm struggling with getting my navigation bar to work properly alongside a slide animation. No matter what I try, the navbar keeps overlapping. Here's the code I have so far: Here is the HTML section: Navbar section: <ul class="slidesho ...

What is the method for retrieving hotels from a database based on their proximity to a specific set of latitude and longitude coordinates?

I have a database table with latitude, longitude, and hotel locations. I want to create a feature that will show hotels near a specific point defined by latitude and longitude. Code Snippet function findNearbyHotels() { $this->lo ...

"Utilize Angular's $http options for requesting instead of using

When I attempt to send a $http POST request to a server API, the request method unexpectedly changes to OPTIONS. Strangely, this issue only occurs when working with localhost. To troubleshoot, I tried making the same request using Postman and it worked fla ...

The Dockerfile for Next13 is unable to locate the server.js file

When using the official Dockerfile from Next with the examples provided, I encounter an error while trying to build the app using a Dockerfile and docker-compose. b78-client-1 | node:internal/modules/cjs/loader:1078 b78-client-1 | throw err; b78-client ...

Customizing the appearance of checkboxes in React Material-Table filtering feature

Is there a way to modify the style of checkboxes in filtering? For instance, if I wish to adjust the dimensions of the checkbox that appears for the Birth Place field, what steps should I take? https://i.stack.imgur.com/pZLqQ.png ...