• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Replace part of string between two spaces

Engaged ,
Jun 30, 2022 Jun 30, 2022

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?

TOPICS
Actions and scripting , macOS

Views

305

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 30, 2022 Jun 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];

 

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 30, 2022 Jun 30, 2022

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 30, 2022 Jun 30, 2022

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 30, 2022 Jun 30, 2022

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 30, 2022 Jun 30, 2022

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)

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 30, 2022 Jun 30, 2022

Copy link to clipboard

Copied

Oh, looks like you have a better way. Very good.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 30, 2022 Jun 30, 2022

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 30, 2022 Jun 30, 2022

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 30, 2022 Jun 30, 2022

Copy link to clipboard

Copied

@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

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 02, 2022 Jul 02, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 07, 2022 Jul 07, 2022

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

LATEST

@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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines