473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Get Text Between - getBtw()

I'm trying to create a function that'll get text in between a string
that returns an Array. It should allow patterm matching so it's
necessary to use the match() function.

However, this function I made doesn't work on MSIE for particularly
unknown reasons. Can anybody take a look at it?

function getBtw(x, y, z) {
var p = new Array();
y = x.match(y);
for (i in y){
x = x.substr(x.indexOf(y[i]) + y[i].length);
z = x.match(z);
if (typeof (z) != "string") { z = z[0]; }
p[i] = x.substring(0, x.indexOf(z));
}
return p;
}

where
x is the entire string,
y is the start pattern,
z is the end pattern

The original function can be found here, but it only returns a string
of the first occurance of the text:
http://javascript-library.blogdrive.com/archive/13.html

I suspect this might be the inconsistency in browser using the match
function.

Jun 11 '06 #1
7 1344
I found the problem, but its still a mystery.

function getBtw(x, y, z) {
var p = new Array();
y = x.match(y);
for (i in y){
x = x.substr(x.indexOf(y[i]) + y[i].length); //x.indexOf(y[i])
returns -1 in MSIE
z = x.match(z);
if (typeof (z) != "string") { z = z[0]; }
p[i] = x.substring(0, x.indexOf(z));
}
return p;

}

at the line where I left a comment, it returns -1 in MSIE. Does anybody
know how to get pass this?

Jun 11 '06 #2
Does anybody have any suggestions?

Seige wrote:
I found the problem, but its still a mystery.

function getBtw(x, y, z) {
var p = new Array();
y = x.match(y);
for (i in y){
x = x.substr(x.indexOf(y[i]) + y[i].length); //x.indexOf(y[i])
returns -1 in MSIE
z = x.match(z);
if (typeof (z) != "string") { z = z[0]; }
p[i] = x.substring(0, x.indexOf(z));
}
return p;

}

at the line where I left a comment, it returns -1 in MSIE. Does anybody
know how to get pass this?


Jun 12 '06 #3
"Seige" <bl******@gmail.com> writes:
Seige wrote:
y = x.match(y);
for (i in y){
x = x.substr(x.indexOf(y[i]) + y[i].length); //x.indexOf(y[i])
returns -1 in MSIE .... at the line where I left a comment, it returns -1 in MSIE. Does anybody
know how to get pass this?
Does anybody have any suggestions?


What strings and regexps are you testing with?
What is the value if x and y[i] where it fails?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 12 '06 #4
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
"Seige" <bl******@gmail.com> writes:
Seige wrote:
y = x.match(y);
for (i in y){
x = x.substr(x.indexOf(y[i]) + y[i].length); //x.indexOf(y[i])
returns -1 in MSIE
Does anybody have any suggestions?

.... What is the value if x and y[i] where it fails?


And, more importantly, what is the value of "i"?

From MSDN:
---
The array returned by the match method has three properties, input,
index and lastIndex.
---
You are using "for(i in y)" to iterator over the properties of the
array in "y", but that array has been extended with three extra
properties.

Try doing "for(i = 0; i < y.length; i++)" instead, then it should
only interate over the actual matched strings.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 12 '06 #5
Thanks Lasse Reichstein Nielsen, that's a nice name.

You're right, I changed it as you suggested and it worked on IE.
However I still don't understand why.

function getBtw(x, y, z) {
var p = new Array();
y = x.match(y);
for (i in y){
x = x.substr(x.indexOf(y[i]) + y[i].length); //x.indexOf(y[i])
returns -1 in MSIE
z = x.match(z);
if (typeof (z) != "string") { z = z[0]; }
p[i] = x.substring(0, x.indexOf(z));
}
return p;
}

Now that this set of codes work, I found several other problems... if
there isn't a match for y or z, it'll fail... And since I'm trying to
avoid using try{}catch(e){}, I'll have to recode it.

Will update here once I get it done.
Lasse Reichstein Nielsen wrote:
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
"Seige" <bl******@gmail.com> writes:
Seige wrote:
y = x.match(y);
for (i in y){
x = x.substr(x.indexOf(y[i]) + y[i].length); //x.indexOf(y[i])
returns -1 in MSIE

Does anybody have any suggestions?

...
What is the value if x and y[i] where it fails?


And, more importantly, what is the value of "i"?

From MSDN:
---
The array returned by the match method has three properties, input,
index and lastIndex.
---
You are using "for(i in y)" to iterator over the properties of the
array in "y", but that array has been extended with three extra
properties.

Try doing "for(i = 0; i < y.length; i++)" instead, then it should
only interate over the actual matched strings.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Jun 15 '06 #6
I fixed it and here is the updated version: Sadly after all the trouble
I went through I didn't use the for() function at all :p

getBtw = function (x, y, z) {
var p = new Array();
while ( x.match(y) && x.match(z) ){
var xy = x.match(y);
if (typeof (xy) != "string") { xy = xy[0]; }
x = x.substr(x.indexOf(xy) + xy.length);
var xz = x.match(z);
if (xz){
if (typeof (xz) != "string") { xz = xz[0]; }
xz = x.indexOf(xz);
p[p.length] = x.substring(0, xz);
x = x.substr(xz);
} //if xz
} //while
return p; }

This function will allow you to get some text between a string and it
supports pattern matching. It'll return first occurance of each match,
and returns an array.

Jun 15 '06 #7
Get Text Between - getBtw()
http://javascript-library.blogdrive.com/archive/13.html
Seige wrote:
I fixed it and here is the updated version: Sadly after all the trouble
I went through I didn't use the for() function at all :p

getBtw = function (x, y, z) {
var p = new Array();
while ( x.match(y) && x.match(z) ){
var xy = x.match(y);
if (typeof (xy) != "string") { xy = xy[0]; }
x = x.substr(x.indexOf(xy) + xy.length);
var xz = x.match(z);
if (xz){
if (typeof (xz) != "string") { xz = xz[0]; }
xz = x.indexOf(xz);
p[p.length] = x.substring(0, xz);
x = x.substr(xz);
} //if xz
} //while
return p; }

This function will allow you to get some text between a string and it
supports pattern matching. It'll return first occurance of each match,
and returns an array.


Jun 15 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jürgen Holly | last post by:
Hi! I have the following xml-node: <docu> <p>Sample: <b>bold</b></p> <p>and text in <i>italic</i></p> </docu> I need to create a text-file, so I set the output-mode to text.
3
by: Xerxes | last post by:
Hi, I need help in setting up a page where the text wraps around an image. Right now, I am using table, with text in one <td> and the image in the adjacent <td>. The problem is when the text is...
2
by: Macsicarr | last post by:
Hi All Wonder if you could help me. I have created a CMS system that allows the user to enter text and pic 'tags' for their own About us page, eg text.... text.... text.... text.......
4
by: Arif Çimen | last post by:
Hi to everybody, I have chnged a button text in design mode. But After compiling and executing the program the text of the button do not change to new value. Any Ideas? Thaks for helps.
3
by: jweinberg1975 | last post by:
I would like for users to be able to select from a small number of options that come from a little drop down menu which then closes. .....
3
by: bbepristis | last post by:
Hey all I have this code that reads from one text file writes to another unless im on a certian line then it writes the new data however it only seems to do about 40 lines then quits and I cant...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
3
by: jonniethecodeprince | last post by:
Hi all, I have trouble getting an array of data stored in a separate javascript file i.e. a file called books.js into a table of data for a .xhtml file. There are 50 Records in this file....
10
by: bluemountain | last post by:
Hi there, Iam new to python forms and programming too I had a text file where i need to extract few words of data from the header(which is of 3 lines) and search for the keyword TEXT1, TEXT2,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.