1

I have a string like this

"/folder1/folder2/folder3/IMG_123456_PP.jpg"

I want to use JavaScript / jQuery to replace the 123456 in the above string with 987654. The entire string is dynamic so cant do a simple string replace. For example, the string could also be

"/folder1/folder2/folder3/IMG_143556_TT.jpg"
"/folder1/folder2/folder3/IMG_1232346_RR.jpg"

Any tips on this?

2
  • Why 123456 is replaced by 987654? Commented Nov 6, 2010 at 19:07
  • is there a logic to the new string that will be inserted ? or all cases need to be replaced with 987654 ? Commented Nov 6, 2010 at 19:08

3 Answers 3

1
"/folder1/folder2/folder3/IMG_123456_PP.jpg".replace(/\_\d{2,}/,'_987654');

Edit :

"/fo1/fo2/fol3/IMG_123456fgf_PP.jpg".replace(/\_\d{2,}[A-Za-z]*/,'_987654');
Sign up to request clarification or add additional context in comments.

3 Comments

Although OP did not specify, but this fails with /folder99/folder88/folder77/IMG_123456_PP.jpg.
but if he reaches folder10 ? :)
Thanks for the help - how can I cover off the situation where the 123456 part of the name have a letter in it - ie. IMG_123456D_PP.jpg ?
1

I am sure there is a better way to do this, but if you are trying to always replace the numbers of that file regardless of what they may be you could use a combination of splits/joins like this:

str = "/folder1/folder2/folder3/IMG_143556_TT.jpg" //store image src in string
strAry = str.split('/') //split up the string by folders and file (as last array position) into array.
lastPos = strAry.length-1; //find the index of the last array position (the file name)
fileNameAry = strAry[lastPos].split('_'); //take the file name and split it into an array based on the underscores.
fileNameAry[1] = '987654'; //rename the part of the file name you want to rename.
strAry[lastPos] = fileNameAry.join('_'); //rejoin the file name array back into a string and over write the old file name in the original string array.
newStr = strAry.join('/');  //rejoin the original string array back into a string.

What this will do is make it so that regardless of what directory or original name of the file name is, you can change it based on the string's structure. so as long as the file naming convention stays the same (with underscores) this script will work.

please excuse my vocab, I know it's not very good heh.

1 Comment

+1 for your work (code, comments) && for being responsible :)
1

Use a regular expression

var str = '/folder1/folder2/folder3/IMG_123456_PP.jpg';

var newstr =  str.replace(/(img_)(\d+)(?=_)/gi,function($0, $1){
                                                  return $1 ? $1 + '987654' : $0;
                                                });

example at http://www.jsfiddle.net/MZXhd/


Perhaps more comprehensible is

var str = '/folder1/folder2/folder3/IMG_123456_PP.jpg';
var replacewith = '987654';
var newstr = str.replace(/(img_)(\d+)(?=_)/gi,'$1'+replacewith);

example at http://www.jsfiddle.net/CXAq6/

2 Comments

The \d+ doesn't need to be lazy because _ is not a digit.
@Kenny, indeed .. but also positive lookbehind does not work in js regex, so i just updated with an alternative..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.