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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:48:34+00:00 2026-05-22T22:48:34+00:00

var str = ‘<div part=1> <div> … <p class=so>text</p> … </div> </div><span></span>’; I got

  • 0
var str = '<div part="1">
    <div>
            ...
        <p class="so">text</p>
            ...
    </div>
</div><span></span>';

I got a long string stored in var str, I need to extract the the strings inside div part=”1″. Can you help me please?

  • 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-22T22:48:34+00:00Added an answer on May 22, 2026 at 10:48 pm

    If you’re using a library like JQuery, this is trivially easy without having to go through the horrors of parsing HTML with regex.

    Simply load the string into a JQuery object; then you’ll be able to query it using selectors. It’s as simple as this:

    var so = $(str).find('.so');
    

    to get the class='so' elememnt.

    If you want to get all the text in part='1', then it would be this:

    var part1 = $(str).find('[part=1]').text();
    

    Similar results can be achieved with Prototype library, or others. Without any library, you can still do the same thing using the DOM, but it’ll be much harder work.

    Just to clarify why it’s a bad idea to do this sort of thing in regex:

    Yes, it can be done. It is possible to scan a block of HTML code with regex and find things within the string.

    However, the issue is that HTML is too variable — it is defined as a non-regular language (bear in mind that the ‘reg’ in ‘regex’ is for ‘regular’).

    If you know that your HTML structure is always going to look the same, it’s relatively easy. However if it’s ever going to be possible that the incoming HTML might contain elements or attributes other than the exact ones you’re expecting, suddenly writing the regex becomes extremely difficult, because regex is designed for searching in predictable strings. When you factor in the possibility of being given invalid HTML code to parse, the difficulty factor increases even more.

    With a lot of effort and good understanding of the more esoteric parts of regex, it can be done, with a reasonable degree of reliability. But it’s never going to be perfect — there’s always going to be the possibility of your regex not working if it’s fed with something it doesn’t expect.

    By contrast, parsing it with the DOM is much much simpler — as demonstrated, with the right libraries, it can be a single line of code (and very easy to read, unlike the horrific regex you’d need to write). It’ll also be much more efficient to run, and gives you the ability to do other search operations on the same chunk of HTML, without having to re-parse it all again.

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

Sidebar

Related Questions

How to append a HTML string such as var str = '<p>Just some <span>text</span>
I will have a string like below var str = -#A This text belongs
var str = $('.rating').html(); $.trim(str); $('.rating').prepend.attr('class', 'rating-' + str.replace(/\s/g, )); <div class=rating>1</div> <div class=rating>2</div>
I have following code (live: http://jsfiddle.net/xyZY8/1/ ): var str = '<div>hello</div><ul><li>Some text...</li>' + '<li>second
I've got an empty object and a string: var obj = {}; var str
var str = 'test TEST'; How to get str = ' <span class=red>test</span> <span
var is:ImageSnapshot = myImagesnapshot; var str:String = ImageSnapshot.encodeImageAsBase64(is); As of now, I am sending
HI I have a URL var str:String = conn=rtmp://server.com/service/&fileId=myfile.flv or var str:String = fileId=myfile.flv&conn=rtmp://server.com/service/
How can I quickly validate if a string is alphabetic only, e.g var str
I need to get all characters between '(' and ')' chars. var str =

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.