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 7811131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:03:13+00:00 2026-06-02T04:03:13+00:00

This is a follow up question to my question about Setting the CSS of

  • 0

This is a follow up question to my question about Setting the CSS of code if it contains a reserved word.

What I am trying to do: If some code has quotes or double quotes, I want to set the color of the font to red and bold. Ex. System.out.println( "Hello world" ); should set “Hello world” to red.

What’s wrong: Despite my best efforts, I can’t seem to get my control statements to work properly (at least I think that’s the issue). It sets the first double quote and beyond to red, but when I tell it to stop when a word equals anyword” or anyword’ it sets the rest of the code in the block to red.

HTML

<html>
    <body>
        <code id="java">
            public static void main(String[] args)<br>
            {
            <pre>    int i = 120; </pre><br>
            <pre>    // Displays a message in the console </pre>
            <pre>    // This is a test </pre>
            <pre>    System.out.println( "Hello Big World!" );</pre>
            }
        </code>
    </body>
</html>

CSS

.quotes
{
    font-weight: bold;
    color: #E01B1B;
}

jQuery

$(document).ready(function() {
    var code  = $("#java").html(); // Get the code
    var split = code.split(' ');   // Split up each element
    var chkQ  = 0;                 // Check for quotes
    var chkC  = 0;                 // Check until end of comment line

    // Set the CSS of reserved words, digits, strings, and comments
    for (var j = 0; j < split.length; j++) {
        // Check to see if chkQ is set to true
        if (chkQ == 1) {
            // If the element matches (anyword") or (anyword'), then set
            // flag to false and continue checking the rest of the code.
            // Else, continue setting the CSS to .quotes
            if (split[j].match(/."/) || split[j].match(/.'/)) {
                split[j] = '<span class="quotes">' + split[j] + '</span>';
                chkQ = 0;
            } else {
                split[j] = '<span class="quotes">' + split[j] + '</span>';
            }
        }
        ...
        } else if (chkQ == 0 && chkC == 0) {
            ...
            // If the element matches a ("anyword) or ('anyword)...
            } else if (split[j].match(/"./) || split[j].match(/'./)) {
                split[j] = '<span class="quotes">' + split[j] + '</span>';
                chkQ = 1;
            } ...
        }
    }
    // Join all the split up elements back together!
    $("#java").html(split.join(' '));
});

Question: Is this just simply an issue with my regex, control blocks or something completely different?

  • 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-06-02T04:03:16+00:00Added an answer on June 2, 2026 at 4:03 am

    Why split the string up when you can perform a simple global regex find and replace:

    <script type="text/javascript">
    $(document).ready(function(){
    //cache the element
       el = $('#java');
    //get the HTML contained within the cached element
       code = el.html();
    //return the code having executed the replace method, regex explained:
    /*    
    ([^\w]{1}) -> look for a single character that is not an alpha character
    (["']) -> then look for either a single quote or double quote
    (.*?) -> then look any character, but don't be greedy
    (\2) -> then look for what was found in the second group - " or '
    ([^\w]{1}) -> and finally look for a single character that is not an alpha character
    */
        code = code.replace(/([^\w]{1})(["'])(.*?)(\2)([^\w]{1})/gm,
    //execute an anonymous callback, passing in the result for every match found
        function(match, $1, $2, $3, $4, $5, offset, original) {
    //construct the replacement
            str =  $1 + '<span class="quotes">' + $2 + $3 + $4 + '</span>' + $5; 
    //return the replacement
            return str; 
        });
    //replace the existing HTML within the cached element
       el.html(code);
    });
    </script>
    

    Edit: Just updated it to accommodate nested quotes.

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

Sidebar

Related Questions

This is a follow-up to a question I asked last year about setting up
This is a follow up question to a previous question I asked about calculating
Follow up to this question about GNU make : I've got a directory, flac
This is a follow on from my previous question although this is about something
Background: This is a follow-up question to this thread about handling EINTR for system
This question is a follow up and continuation of this question about a Privilege
This is a follow-up to my previous question about developing Perl applications . Let’s
This question is a follow-up on a question about Python variable scope . Additional
Disclaimer: This is a follow-on question from my other question about NServiceBus which was
Basically this is a follow up of this question about most vexing parse. I

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.