Skip to main content
Inspiring
June 30, 2022
Answered

Replace part of string between two spaces

  • June 30, 2022
  • 6 replies
  • 787 views

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?

This topic has been closed for replies.
Correct answer Chuck Uebele

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];

 

6 replies

pixxxelschubser
Adobe Expert
July 7, 2022

@milevic 

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.

Stephen Marsh
Adobe Expert
July 3, 2022

@milevic – so how did the regular expression based solutions offered by myself and pixxxelschubser work for you?

milevicAuthor
Inspiring
July 7, 2022

Thank you all for your dedication, honestly i successfuly resolved this issue with @Chuck Uebele code so i didnt try any else solution.

pixxxelschubser
Adobe Expert
June 30, 2022

@milevic 

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

 

 

Stephen Marsh
Adobe Expert
June 30, 2022

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.

Chuck Uebele
Chuck UebeleCorrect answer
Adobe Expert
June 30, 2022

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];

 

milevicAuthor
Inspiring
June 30, 2022

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?

milevicAuthor
Inspiring
June 30, 2022

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)

 

Chuck Uebele
Adobe Expert
June 30, 2022

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.