Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trim Function

Status
Not open for further replies.

j1500

Programmer
Feb 15, 2006
10
US
I need to trim the values inbetween the () parens in the following string. The number of characters between the parens may vary so I basically need to search for the beginning ( and ending ) and extract the value.

5609010.1151607160.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)

Please provide any guidance you may have. I noticed JavaScript does not have a trim function.

 
See this is a cookie value and I only want the value (whatever it may be; in this case it is direct) inbetween the parens of 'utmccn='.

So, I need a function which would return "direct" extracted from the string below:

5609010.1151607160.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)

 
well, here's an example, using a regular expression. this will work, however depending on the complexity of your cookie string, you may need to tweak the regex a little...

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Untitled</title>
<script type="text/javascript"><!--

var cookie = "5609010.1151607160.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)";
var ma = cookie.match( /(\S+)\((\w+)\)(\S+)\((\w+)\)(\S+)\((\w+)\)/ );

alert(RegExp.$2);
alert(RegExp.$4);
alert(RegExp.$6);

//--></script>
</head>

<body>

</body>
</html>



*cLFlaVA
----------------------------
[tt]( <P> <B>)[sup]13[/sup] * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Here's an alternate version of Cory's code that will handle a dynamic number of values.
Code:
<script type="text/javascript"><!--

var cookie = "5609010.1151607160.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)";
var ma = cookie.match( /[^\(\)]+(?=\))/gi );

for(var i=0; i<ma.length;i++){
  alert(ma[i]);
}

//--></script>

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top