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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:34:47+00:00 2026-06-08T03:34:47+00:00

I’m trying to figure out how to split a string using regex while having

  • 0

I’m trying to figure out how to split a string using regex while having the results placed into a hash table.

Example:

var x = "Name: John Doe
Age: 30
Birth Date: 12/12/1981";

var arr = x.split(/Name:|Age:|Birth Date:/);

However while I can spit a string the problem is I can’t store the values into a useful format such as a hash etc. Since some of the information may not always be shown.

I would want the results to be something like:

var myHash = {}; // New object
myHash['Name'] = "John Doe";
myHash['Age'] = "30";
myHash['Birth Date'] = "12/12/1981";

Is there an easy why to do this?

Edit: The script is used to parse data from a generated report. And the format does not always have carriage returns. It’s going to be used to automatically generate notices instead of hand typing everything.

Example:

Name: John Doe       Birth Date: 12/12/1981
Age: 30

However, after seeing examples I may be able to do it if I first add a carriage return or other special character in front of the regex matches. Need to figure out how to add a value in front of the regex matches first.

  • 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-08T03:34:51+00:00Added an answer on June 8, 2026 at 3:34 am

    You can use two splits to first split it into items and then to split each item:

    var results = {};
    var x = "Name: John Doe\nAge: 30\nBirth Date: 12/12/1981";
    var pieces = x.split("\n"), part;
    for (var i = 0; i < pieces.length; i++) {
        part = pieces[i].split(":");
        results[part[0]] = part[1];
    }
    

    Working example: http://jsfiddle.net/jfriend00/kYwq6/

    Depending upon exactly what you’re doing, you may want to trim the whitespace off beginning and end of each key and value before putting it into the results object.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.