0

For example

let myString = "This is my string";

let replacedString = myString.replace(/\ /g, "") //Thisismystring

Now that all the whitespaces have been removed, how do I put them back in the exact position?

Additionally, let's suppose the replaced string undergoes some change and becomes

let myChangedString = "(T)(h)(i)(s)(i)(s)(m)(y)(s)(t)(r)(i)(n)(g)";

Now I want to put the whitespaces back where they used to be i.e after (s) and before (i), after (s) and before (m), after (y) before (s)

I've spent a couple of hours on this and been stuck in the same position, any form of help would be greatly appreciated.

EDIT: Solved, thank you very much.

3
  • Provided you have something already, then please share it here. Show what you tried. Commented Feb 26, 2021 at 17:18
  • why not just save an old version of the string into a variable, to be used to restore the spaces? Commented Feb 26, 2021 at 17:25
  • how do you change the string after removing the whitespace? Commented Feb 26, 2021 at 17:44

4 Answers 4

1

The trick here is to replace the spaces with another character - rather than just removing the space. That way - its a simple matter to replace the added character with a space to return the spaces to where they started. When I do this - I always use the tilde character "~" since it is easily recognisable as well as unlikely to actually be used in a string.

I have added a few variations / modifications as well as the example you have provided with every character being wrapped in parentheses - just note that you will need to escape these when replaceing the (~) for the " " space character.

let myString = "This is my string";

let replacedString = myString.replace(/\ /g, "~");
console.log(replacedString);//This~is~my~string

let modifiedString = replacedString.replace(/my/g, "your");
console.log(modifiedString);//This~is~your~string

let spacedString = modifiedString.replace(/~/g, " ");
console.log(spacedString);//This is your string



// using your example of wrapping each character in parentheses

let myChangedString = "(" + modifiedString.split('').join(")(") + ")";
console.log(myChangedString); //(T)(h)(i)(s)(~)(i)(s)(~)(y)(o)(u)(r)(~)(s)(t)(r)(i)(n)(g)


let mySpacedString = myChangedString.replace(/\(~\)/g, " ");
console.log(mySpacedString); //(T)(h)(i)(s) (i)(s) (y)(o)(u)(r) (s)(t)(r)(i)(n)(g)

Sign up to request clarification or add additional context in comments.

5 Comments

this would work. although you end up having 3 copies of the string. one with spaces, one with tilde and one with no spaces. Seems like you can accomplish the same thing with just keeping 2 copies of the string (spaces and no spaces). But depending on the use, having a 3rd version with the tilde might be helpful.
@Rick - sorry - I would say you only have two copies - one with spaces and one with tildes and if I was doing this, I would be replacing the string itself - rather than creating copies....so there would be one string in either of two states - spaced or tllded.... and the main reason for doing this would be to allow other manipulations of the string than simply swapping the spaces.. removuing entire words - swapping order of words etc.... having the tilde allows replacement to the space irrespective of the sting modifications.
yes I understand. But what does the tilde accomplish? Its a character just like a space is a character. What kind of string manipulation can you achieve with the tilde that you cannot achieve with the space?
@Rick - there are numerous examples - joinig words together - “I did it my self and am proud” .... changed to “ I did it myself and am proud” or contractions - “I did not do it.” ..... “I didn’t do it.” - its just easier to replaccw a non space character than to compare the new string to the original to determine if and where the spaces should be placed.~~~👍
I guess I don't see it. you are going to end up with replace(/my~self/g, 'myself') versus replace(/my self/g, 'myself'). But if it makes sense to you, I say go with it.
1

Why not replace only the parts you need to be replaced?

For example search for word character and replace with the wanted parts.

console.log("This is my string".replace(/\w/g, '($&)'));

Comments

0

Better you just transform your original array. Loop through array and modify the char is not empty.

let myString = "This is my string";

let chars = [...myString].map(item => item !== ' ' ? '(' + item + ')':  item)

console.log(chars.join(''))

Comments

0

Are you looking for this...

var result = "thisismystring".replace(/^(.{4})(.{2})(.{2})(.*)$/, "$1 $2 $3 $4");
alert(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.