0

I trying to replace spaces with comma for words that only have space between them like this example

Color : NavyBlue TrainLength : FloorLength USSize : 6 shipping : standard

it should be like this

Color : NavyBlue,TrainLength : FloorLength,USSize : 6,shipping : standard

where it has commas between these variants

I can only match spaces between words that have colons

\s*:\s*

but I need a way to match spaces between words that has no colon

for example consider the dots as space

don't match this ...:...

but match this NavyBlue.....TrainLength

match the spaces only not the words

to test it test real example

4
  • .replace(/([a-z0-9]) ([a-z0-9])/gi, "$1,$2"); Commented Jan 19, 2020 at 20:51
  • @ASDFGerte didn't work try from the link I updated in the question Commented Jan 19, 2020 at 20:56
  • Well, if you change the format, of course a regex written for the original one is highly likely to not work... Commented Jan 19, 2020 at 20:58
  • @ASDFGerte What I need is to match the space between the variants to replace it with comma like the example I updated but in your example it will match (words space words ) which I don't want to match any words Commented Jan 19, 2020 at 21:01

1 Answer 1

1

This should work:

let s1 = " Color  :  NavyBlue      TrainLength : FloorLength  USSize : 6   shipping";
let r1 = s1.replace(/(\w+)\s+(\w+)/g, "$1,$2");
console.log(r1);

let s2 = "Color : NavyBlue TrainLength : FloorLength USSize : 6 shipping : standard";
let r2 = s2.replace(/(\w+)\s+(\w+)/g, "$1,$2");
console.log(r2);

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

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.