-2

I have a string in JavaScript and at some places corresponding to a regex (lower case followed by upper case), I would like to insert between upper and lower case the backspace character.

This is an example:

Manufacturer: SamsungWarranty: 12 monthsUnlocking: Any operatorIris scanner: NoScreen size: 5.7 inchesDisplay resolution: 2560 x 1440 pixels

It should become:

Manufacturer: Samsung

Warranty: 12 months

Unlocking: Any operator

Iris scanner: No

Screen size: 5.7 inches

Display resolution: 2560 x 1440 pixels
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 2
    Where is the string you need to parse, we would need the string and the desired outcome – nalnpir Jun 13 '19 at 15:30
  • This kind of string "FooFooFoo" I would like this to become "Foo Foo Foo" Insert backspace within o and F – Samaritain Sim'S Jun 13 '19 at 15:32
  • You can `split` at each case your regex match – zronn Jun 13 '19 at 15:33
  • Possible duplicate of [How do I split a string with multiple separators in javascript?](https://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript) – zronn Jun 13 '19 at 15:34
  • How can i split ? With which regex. This is a better example Manufacturer: SamsungWarranty: 12 monthsUnlocking: Any operatorIris scanner: NoScreen size: 5.7 inchesDisplay resolution: 2560 x 1440 pixels It should become Manufacturer: Samsung Warranty: 12 months Unlocking: Any operator Iris scanner: No Screen size: 5.7 inches Display resolution: 2560 x 1440 pixels – Samaritain Sim'S Jun 13 '19 at 15:36

1 Answers1

1

You can match on /([a-z])([A-Z])/g and replace with "$1\n\n$2", then prepend "Manufacturer: " to the beginning of the string.

const s = "SamsungWarranty: 12 monthsUnlocking: Any operatorIris scanner: NoScreen size: 5.7 inchesDisplay resolution: 2560 x 1440 pixels";
const res = "Manufacturer: " + s.replace(/([a-z])([A-Z])/g, "$1\n\n$2");
console.log(res);
ggorlen
  • 44,755
  • 7
  • 76
  • 106