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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trim a string w/ js 2

Status
Not open for further replies.

meriwether

Programmer
Mar 7, 2003
23
US
i need to remove the first and last character off a string.

i've got a number wrapped in parenthesis, and want the parenthesis gone.

example:

(01) i'd like to change it to just 01.

any ideas? much appreciated...
 
Code:
var str = "(10)";
var nstr = str.substr(1,2);
alert(nstr);

-pete


 
pete that works great, however i then ran into values like:

this is a test (01)
salt lake city (02)
new york (93)

most of the values do not have spaces and are like:

sacramento (01) which the above works on, but a few do, so that throws a monkey wrench into it.

so far i've got it broken down such as

salt lake city
and
01)

need to remove the last hanging ")"...

what i've got coded:

var foo = document.form.fieldName1.options[documentform.fieldName1.selectedIndex].text;
foo = foo.split("(");
foo1 = foo[0];
foo2 = foo[1];
// foo2 = foo2.split(")");
// foo2 = foo2.substr(1,2);
document.form.fieldName2.value = foo1;
document.form.fieldName3.value = foo2;
}

thanks again for the quick assistance.
 
one other issue,
actually there are values such as (127)

chicago (127) so foo2.substr(0,2) will chop off the 7...
 
got it:

foo2 = foo2.substr(0, foo2.length -1);

thanks pete, your answer got the ball rolling. :)
 
People forget the power of Regular Expressions:

document.write( &quot;<br>Moosehead (01)&quot;.replace(/\((\d+)\)/, &quot;$1&quot;) )
document.write( &quot;<br>BeverlyBil (90210)&quot;.replace(/\((\d+)\)/, &quot;$1&quot;) )
document.write( &quot;<br>Alexander (1712)&quot;.replace(/\((\d+)\)/, &quot;$1&quot;) )



Gary Haran
==========================
 
Gary,

>> People forget the power of Regular Expressions:

I didn't forget, look at how the requirements changed during the thread! LOL

If he would have posted the requirements the first time i would have used regular expressions as the solution for sure! [hammer]

-pete

 
xutopia,

thanks for reminding me how to use backreferences with replace()...

=========================================================
while (!succeed) try();
-jeff
 
Pete,

Sorry I guess it helps when someone just reads the last post because we can easily be carried away from the first ones onward. I probably wouldn't have thought about it if I had read the thread from the beggining! :)

jeff,

Yeah someone reminded me about that just recently so I guess I'm just passing on the favor.

Everyone,

$1..$9 are especially helpful when you want to swap things. One friend showed me this example just a few days ago.

alert(&quot;Jill Jack&quot;.replace(/(\w+)\s(\w+)/, &quot;$2 $1&quot;));

;-)

Gary Haran
==========================
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top