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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:53:53+00:00 2026-06-10T23:53:53+00:00

I’m having problems with regular expressions in JavaScript. I’ve got a number of strings

  • 0

I’m having problems with regular expressions in JavaScript. I’ve got a number of strings that need delimiting by commas. Unfortunately, the sub strings don’t have quotes around them which would make life easier.

var str1 = "Three Blind Mice 13 Agents of Cheese Super 18"
var str2 = "An Old Woman Who Lived in a Shoe 7 Pixies None 12"
var str3 = "The Cow Jumped Over The Moon 21 Crazy Cow Tales Wonderful 9"

They are in the form of PHRASE1 (Mixed type with spaces”) INTEGER1 (1 or two digit) PHRASE2 (Mixed type with spaces”) WORD1 (single word mixed type, no spaces) INTEGER2 (1 or two digit)

so I should get:

result1 = "Three Blind Mice,13,Agents of Cheese,Super,18"
result2 = "An Old Woman Who Lived in a Shoe,7,Pixies,None,12"
result3 = "A Cow Jumped Over The Moon,21, Crazy Cow Tales,Wonderful,9"

I’ve looked at txt2re.com, but can’t quite get what I need and ended up delimiting by hand. But I’m sure it can be done, albeit someone with a bigger brain. There are lots of examples of regEx but I couldn’t find any to deal with phrases; so I was wondering if anyone could help me out. Thank you.

  • 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-10T23:53:55+00:00Added an answer on June 10, 2026 at 11:53 pm

    Here’s an attempt at a regular expression that works for your example strings:

    /^\b((?:[a-z]+ ?)+)\b (\d{1,2}) \b((?:[a-z]+ ?)+)\b (\b[a-z]+\b) (\d{1,2})$/i
    

    Basically, it consists of five different parts, each designed to match your descriptions:

    1. \b((?:[a-z]+ ?)+)\b = Matches words consisting of a-z, optionally followed by a space. We then quantify this as many times as possible (the i-flag makes the search case insensitive)
    2. (\d{1,2}) = Matches 1 or two digits. Could also be written as [0-9].
    3. \b((?:[a-z]+ ?)+)\b = Same as nr 1.
    4. (\b[a-z]+\b) = Matches a single word consisting of a-z
    5. (\d{1,2}) = Same as nr 2.

    Things to note:

    1. I have anchored the match to ensure it only considers the full string using ^ (start of subject) and $ (end of subject).
    2. After the regex delimiters /.../, I have added a flag that alters how the regex engine behaves. The i-flag makes the match Case-Insensitive.
    3. A caveat is that contractions like “it’s” will NOT be matched by the current regex. You will need to modify regex group 1 and 3 to accommodate this.
    4. The groups are separated by a single space. IF this can vary, then you need to modify the group separators.

    You can use it as follows:

    var regex = /^\b((?:[a-z]+ ?)+)\b (\d{1,2}) \b((?:[a-z]+ ?)+)\b (\b[a-z]+\b) (\d{1,2})$/i;
    var s = "The Cow Jumped Over The Moon 21 Crazy Cow Tales Wonderful 9";
    s = s.replace(regex, '$1, $2, $3, $4, $5');
    

    JS-fiddle demo here

    Edit: I’ve updated the demo to create a variable named resultCollection to hold the processed results. It’s an object consisting of each original string as key, and the processed resulting string as the value.

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

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

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.