Solved

regexp in java script to get text between tags

Posted on 1998-07-31
3
355 Views
Last Modified: 2008-02-07
I've been using the folowing to strip out content in a document thats between certain tags and return the results in an array. There are multiple instances of this tag.
What I've been using looks like;

arrA = data.match(/<DATA>(.*)<\/DATA>/gi);
      
for (x=0; x<arrA.length; x++)
      Response.Write(arrA + "<BR>");

But all this does is return 1 result between the first data tag and the very last tag. Ignoring the tags in between. I should get a number of results.

I hope this makes sense


0
Comment
Question by:matthewallum
3 Comments
 
LVL 1

Expert Comment

by:someu
ID: 1267511
How are you going to use the result??
It's for making a function or not.
someu@hotmail.com
0
 

Author Comment

by:matthewallum
ID: 1267512
I made a mistake in the line above, it should read;

Response.Write(arrA[x] + "<BR>");

sorry.
0
 
LVL 2

Accepted Solution

by:
bernfarr earned 100 total points
ID: 1267513
The way that the regular expression is written it would only return one value, I believe. Did you look at what was printed by your code?

I think you need to process the result with a further match operation where you specify the separators between your data tokens. In other words, what character, or characters separate your data values.  Assuming that each token was separated by a newline, you would need code something like: (This is untested)

allData = data.match(/<DATA>(.*)<\/DATA>/gi);

arrA = allData.match(/(\w+)$/gi);

for (x=0; x<arrA.length; x++)
  Response.Write(arrA + "<BR>");


Note that I always find regular expressions a pain to get right. Using Emacs, you can test them on a regular text file first.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now