how do i find a string between 2 delimiters please

P

Phil

hello

I am a trying to find the text between a ? mark and an = sign:
var theData;
var begin;
var thePageNo;
var beginFrom;
beginFrom = top.location.href.indexOf("=");
begin = top.location.href.indexOf("?");
if (begin > 0 )
{
theData = top.location.href.substring(begin+1,location.href.length);
theData = unescape(theData);
..........
--
what I need to do is pass the filename without the + sign and whatever
follows it.

So if the string passed is:
http://philippeoget.50megs.com/?IT_Study___Information.htm=13

I want to be able retrieve only the string up and including the = sign.
ie.: http://philippeoget.50megs.com/?IT_Study___Information.htm

I tried:
theData = top.location.href.substring(begin+1,location.href.length-beginFrom);

but it doesn't like it..

TIA
Regards

Phil
 
K

kaeli

hello

I am a trying to find the text between a ? mark and an = sign:
var theData;
var begin;
var thePageNo;
var beginFrom;
beginFrom = top.location.href.indexOf("=");
begin = top.location.href.indexOf("?");
if (begin > 0 )
{
theData = top.location.href.substring(begin+1,location.href.length);
theData = unescape(theData);
.........

This worked for me. It's extensible so the URL can have more params. It
assumes the one you want is the first part of the first pair ([0]).

<html>
<head>
<title> New Document </title>
</head>

<body>
<script language="javascript" type="text/javascript">
// split the query string into param=val pieces
var qs = location.search.substr(location.search.indexOf("?")+1);
var pieces = qs.split("&"); // pieces has all the name/value pairs

var piece = null;
if (typeof pieces[0] != "undefined") piece = pieces[0].split("="); //
piece has individual name/values for pieces[0]

if (typeof piece[0] != "undefined") url = self.location.href+piece[0];
alert(url);
</script>
</body>
</html>

--
 
S

Shawn Milo

So if the string passed is:
http://philippeoget.50megs.com/?IT_Study___Information.htm=13

I want to be able retrieve only the string up and including the = sign.
ie.: http://philippeoget.50megs.com/?IT_Study___Information.htm

I tried:
theData = top.location.href.substring(begin+1,location.href.length-beginFrom);

but it doesn't like it..

TIA
Regards

Phil

The easiest way to do this is with a regular expression.

I just wrote and tested this:

var someString =
'http://philippeoget.50megs.com/?IT_Study___Information.htm=13';

document.write(someString + '<br/>');
someString = someString.replace(/^.*\?(.*)=.*$/, '$1');
document.write(someString + '<br/>');



Shawn
 
S

Shawn Milo

Phil,

Although my last post provided a working solution,
I realized that there is a flaw in my logic. I was
assuming that only one name/value pair will ever
be passed in the querystring. Please see below
for a correction.

For regex (regular expression) info, this
book is the best source:
http://www.oreilly.com/catalog/regex2/



Shawn




var someString =
'http://philippeoget.50megs.com/?IT_Study___Information.htm=13';

//altered to further illustrate regex
someString =
'http://philippeoget.50megs.com/?IT_Study___Information.htm=13&lang=en';


//show original string
document.write(someString + '<br/>');

//perform replace
someString = someString.replace(/^.*\?(.*)=.*$/, '$1');

//show altered string
document.write(someString + '<br/>');


//
someString = '';
//someString = 'http://www.abc.com?page=it_study&lang=en';



//Note, this regex will work when there are more parameteres
//in the querystring than just one. The other one will not.
//Example:
http://philippeoget.50megs.com/?IT_Study___Information.htm=13&lang=en
//The previous regex will return:
IT_Study___Information.htm=13&lang
//This one will return: IT_Study___Information.htm

someString = someString.replace(/^.*\?([^=]*)=.*$/, '$1');
document.write(someString + '<br/>');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top