Solved

regexp in java script to get text between tags

Posted on 1998-07-31
3
356 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

778 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