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

How to modify a string? Need help.

Status
Not open for further replies.

dwest100

Technical User
Aug 9, 2003
59
US
Hi,
I'm hacking and I'm not a programmer. I have isolated what I need to do though.

I have a variable called filename which contains a value of say "001_001.jpg".

Using javascript, how would I change the value for filename to be "001_001.thumbnail.jpg"?

Once I know this, I think I can add it into my existing code and make it work.

Thanks!

 
use the replace method coupled with a regular expression:

Code:
<script type="text/javascript">

var str = "001_001.jpg";
str = str.replace(/\.jpg/g, ".thumbnail.jpg");
alert(str);

</script>

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Thanks for the reply!

I've reworked the code a little. What I need now is a statement to REMOVE the ".thumbnail" from a file name like this:

100_100.thumbnail.jpg

needs to be transformed to this:

100_100.jpg

I know it would be something like this:

newname = oldname.replace(/\.jpg/g, ".jpg");

Problem is, I am clueless on regex. What would the regex need to be??
 
Rethink:
Here is what I would like to do with the javascript:

Given string: "001_001.thumbnail.???" where ??? could be any common image file extension.

Change the filename to
"001_001.??? where ??? is the same extension as the original.

In other words, remove ".thumbnail" but leave the rest as in the original.

Thanks!

 
Nevermind!
I figured it out.
str.replace(".thumbnail", "")

Thanks and sorry for my excess posts!
 
[purple]Glad I could help[/purple] [wink]

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top