Copy link to clipboard
Copied
I have some psds which file names are in this format XX XXXX XX XXX. How I can, using javascript, replace the part of string which is between first and second space to get this kind of output string XX YYYY XX XXX?
I'm not on my computer, so not sure how this will work out. Using a space with the split, will remove all the spaces, so you have to put them back, when you concatenate the string.
var orgStr = 'XX XXXX XX XXX';
var strA = orgStr.split(' ');
var newStr = strA[0] + ' YYYY ' + strA[2] + ' ' + strA[3];
Copy link to clipboard
Copied
I'm sure there's a way using regex, but I'm not good at that. What I would do is use the split command, using a space as the character to split the string into an array. Then you just piece the string back together using the elements of the array, except for the second element between the first two spaces, you put in the replacement.
Copy link to clipboard
Copied
I'm not on my computer, so not sure how this will work out. Using a space with the split, will remove all the spaces, so you have to put them back, when you concatenate the string.
var orgStr = 'XX XXXX XX XXX';
var strA = orgStr.split(' ');
var newStr = strA[0] + ' YYYY ' + strA[2] + ' ' + strA[3];
Copy link to clipboard
Copied
It works 🙂 but i have some additional issue, what happend in situation when i dont know how words name will contain, for example somethimes string have 5 words somethimes 10, but anyways my needs are always to change second word?
Copy link to clipboard
Copied
Now i understood your idea and i tried with this and it works...
var orgStr = 'XX XXXX XX XXX';
var strA = orgStr.split(' ');
strA[1]= 'YYYY';
var newStr = strA.join(' ');
alert(newStr)
Copy link to clipboard
Copied
Oh, looks like you have a better way. Very good.
Copy link to clipboard
Copied
If you have different word amounts, you would create a loop based on the harrah's length, to put the string back together.
Copy link to clipboard
Copied
Regular Expression find & replace with Capture Groups.
This version is very flexible, it is only looking for patterns with word spaces, it could be any character or any amount of characters.
var origString = "XX XXXX XX XXX";
alert(origString);
var newString = origString.replace(/(.+ )(.+)( .+)( .+)/, '$1YYYY$3$4');
alert(newString);
While this version is tighter/more explicit, it is looking for a pattern of 2 characters and space, four characters, space and two characters and space and three characters:
var origString = "XX XXXX XX XXX";
alert(origString);
var newString = origString.replace(/(.{2} )(.{4})( .{2})( .{3})/, '$1YYYY$3$4');
alert(newString);
One could of course swap the "any character" . metacharacter for alpha or numeric/digit characters etc.
Copy link to clipboard
Copied
Do you have two or more real examples?
var t1 = "Do find and replace";
var t2 = "WhatEverYouWant";
var t3 = t1.replace( /(\S+\s)(\w+)/, '$1'+t2);
alert(t3); // result: Do WhatEverYouWant and replace
Copy link to clipboard
Copied
@milevic – so how did the regular expression based solutions offered by myself and pixxxelschubser work for you?
Copy link to clipboard
Copied
Thank you all for your dedication, honestly i successfuly resolved this issue with @Chuck Uebele code so i didnt try any else solution.
Copy link to clipboard
Copied
I find that a great pity.
Because especially when it comes to scripting file names or the file system in general, it is usually a no-go to use spaces in the file name. And then it is very advantageous to be able to fall back on additional variants when searching.
Knowledge can only be replaced by one thing - even more knowledge.