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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:32:44+00:00 2026-06-17T17:32:44+00:00

I want to parse strings using javascript with two alternative formats: id#state#{font name, font

  • 0

I want to parse strings using javascript with two alternative formats:

id#state#{font name, font size, "text"}  
// e.g. button1#hover#{arial.ttf, 20, "Ok"}

or

id#state#text                            
// e.g. button1#hover#Ok

where in the second variant, a default font and size is being assumed.

Before you read further, I have to point out that I control the format, so I’d love to hear about any other format that is more RegExp Friendly™. That being said, the second alternative is needed for historical reasons, as is the id#state#-part. In other words, the flexibility resides in the {font name, font size, "text"}-part.

Furthermore, I’d like to use RegExp as far as possible. Yes, the RegExp I suggest below is pretty hairy, but for my case this is not only a possible solution to the problem at hand but also a matter of learning more about RegExp itself.

My current attempt at grouping the three or alternatively five information elements in the two formats is as follows.

var pat = /^(\w*)#(\w*)#
          (?:(?:\{([\w\.]*),\s*([0-9\.]*),\s*"([\w\s]*)"\})|([\w\s]*))$/;

var source1 = "button1#hover#{arial.ttf, 20, \"Ok\"}";
var source2 = "button1#hover#Ok";

var result1 = source1.match ( pat );
var result2 = source2.match ( pat );

alert ( "Source1: " + result1.length + " Source2: " + result2.length );

When I tested this expression at http://www.regular-expressions.info/javascriptexample.html, I got:

result1 = [ button1#hover#{arial.ttf, 20, "Ok"}, button1, hover, arial.ttf, 
            20, Ok, undefined ]

and

result2 = [ button1#hover#Ok, button1, hover, undefined, 
            undefined, undefined, Ok ]

Here’s how I break down the RegExp:

^(\w*)#(\w*)#(?:(?:\{([\w\.]*),\s*([0-9\.]*),\s*"([\w\s]*)"\})|([\w\s]*))$

^                 # anchor to beginning of string
(\w*)             # capture required id
#                 # match hash sign separator
(\w*)             # capture required state
#                 # match hash sign separator
                  # capture text structure with optional part:
(?:(?:\{([\w\.]*),\s*([0-9\.]*),\s*"([\w\s]*)"\})|([\w\s]*))  
$                 # anchor to end of string

The text structure capture is the dodgiest part, I guess. I break it down as follows:

(?:                  # match all of what follows but don't capture
    (?:\{            # match left curly bracket but don't capture (non-capturing group)
          ([\w\.]*)  # capture font name (with possible punctuation in font file name)
          ,\s*       # match comma and zero or more whitespaces
          ([0-9\.]*) # capture font size (with possible decimal part)
          ,\s*"      # match comma, zero or more whitespaces, and a quotation char
          ([\w\s]*)  # capture text including whitespaces
    "\})             # match quotation char and right curly bracket (and close non-capturing group)
    |                # alternation operator
    ([\w\s]*)        # capture optional group to match the second format variant
)                    # close outer non-capturing group

My question is two fold:

1) How can I avoid the trailing undefined match in the result1 case?

2) How can I avoid the three undefined matches in the middle of the result2 case?

Bonus question:

Did I get the break down right? (I guess there is something amiss, since the RegExp isn’t working entirely as expected.)

Thanks! 🙂

  • 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-17T17:32:45+00:00Added an answer on June 17, 2026 at 5:32 pm

    The groups in your regex are numbered from left to right without regard for the operators (in particular, the | operator). When you’ve got (x)|(y) then the group for either “x” or “y” will be undefined.

    Thus you can’t avoid the empty slots in the result. In fact, I think you want them, because otherwise you don’t really know which form of input you’ve matched.

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

Sidebar

Related Questions

I am using opencsv to parse text file and generate List<String[]> now I want
I want to parse date strings like February 7, 2011 using M dd, yyyy
The issue I have is that I want to parse strings in order, so
I have strings with dates, and want to parse these into NSDate objects. Is
I'm trying to parse and amend some html (as a string) using javascript and
I'm using ANTLR to parse strings of mathematical expressions and tag them using MathML.
I need parse assembly qualified name without using AssemblyName, because I get System.IO.FileLoadException (the
I'm writing a little program in JavaScript in which I want to parse the
I want to parse a string that represent a DateTime in UTC format. My
Let's say I want to parse a string with various opening and closing brackets

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.