By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

how do i find a string between 2 delimiters please

P: n/a
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_S...rmation.htm=13

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

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

but it doesn't like it..

TIA
Regards

Phil
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <f6**************************@posting.google.com >,
ph**********@yahoo.com enlightened us with...
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>

--
--
~kaeli~
Shotgun wedding: A case of wife or death.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2

P: n/a


Regards

Philippe
http://uk.geocities.com/philippeoget/

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3

P: n/a
<snip>
So if the string passed is:
http://philippeoget.50megs.com/?IT_S...rmation.htm=13

I want to be able retrieve only the string up and including the = sign.
ie.: http://philippeoget.50megs.com/?IT_S...nformation.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
Jul 23 '05 #4

P: n/a
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_S...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/>');
Jul 23 '05 #5

P: n/a
Thank you very much for your help.

Regards

Philippe
http://uk.geocities.com/philippeoget/

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.