Solved

regexp in java script to get text between tags

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

760 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

24 Experts available now in Live!

Get 1:1 Help Now