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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:31:22+00:00 2026-05-15T18:31:22+00:00

Hey I’m trying to do something quite specific with regex in javascript and my

  • 0

Hey I’m trying to do something quite specific with regex in javascript and my regexp-foo is shakey at best. Wondered if there were any pros out there who could point me in the right direction. So I have some text…

<item id="myid1">myitem1</item>
<item id="myid2">myitem2</item>

…etc

And I would like to strip it out into an array that reads
myid1, myitem1, myid2, myitem2, ….etc

There will never be nested elements so there is no recursive nesting problem. Anyone able to bash this out quickly?
Thanks for your help!

  • 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-05-15T18:31:23+00:00Added an answer on May 15, 2026 at 6:31 pm

    Here’s a regex that will:

    • Match the starting and ending tag element names
    • Extract the value of the id attribute
    • Extract the inner html contents of the tag

    Note:
    I am being lazy in matching the attribute value here. It needs to be enclosed in double quotes, and there needs to be no spaces between the attribute name and its value.

    <([^\s]+).*?id="([^"]*?)".*?>(.+?)</\1>
    

    Running the regex in javascript would be done like so:

    search = '<item id="item1">firstItem</item><item id="item2">secondItem</item>';
    regex = new RegExp(/<([^\s]+).*?id="([^"]*?)".*?>(.+?)<\/\1>/gi);
    matches = search.match(regex);
    results = {};
    for (i in matches) {
        parts = regex.exec(matches[i]);
        results[parts[2]] = parts[3];
    }
    

    At the end of this, results would be an object that looks like:

    {
        "item1": "firstItem",
        "item2": "secondItem"
    }
    

    YMMV if the <item> elements contain nested HTML.

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

Sidebar

Ask A Question

Stats

  • Questions 467k
  • Answers 467k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can find some information about installing LaTeX at https://help.ubuntu.com/community/LaTeX,… May 16, 2026 at 1:54 am
  • Editorial Team
    Editorial Team added an answer Have you changed your publish settings? Open: File -> Publish… May 16, 2026 at 1:54 am
  • Editorial Team
    Editorial Team added an answer In your settings.xml file, you'll want under <w:settings/> an element… May 16, 2026 at 1:54 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.