1

I want to split both the following string and put the index number in each with the . (dot) at the end.

let str1 = "1. sun ; moon ; star ; god ; goddess";

// will work with both the string

let str2 = "sun; moon; star; god; goddess;";

Result should be like this

let result = "1. sun\n2. moon\n3. star\n4. god\n5. goddess.";

Or as below if executed

1. sun.
2. moon.
3. star.
4. god.
5. goddess.

Update: I splitted it but failed to put the index number in each word. Since the words are random e.g. one can have 3 words and other can have the 5 words and so on...

1
  • I already tried it but failed to put the index number in each word. Commented Nov 10, 2019 at 16:24

2 Answers 2

1

You can achieve that by removing the list numbers from the string before adding them back. Here is an example :

const formatList = list => {
  list = list
    // split the string
    .split(';')
    // filter out empty list items
    .filter(Boolean)
    // Iterate over the list items to format them
    .map((item, index) => {
      // Start with the index (+1 one to start from 1)
      return (index + 1)
        // Add the dot and the space
        +
        '. '
        // Add the list item without any number+dot substring and any extra space
        +
        item.replace(/\d+\./g, '').trim()
        // Add a final dot (even if list should not usually have ending dots)
        +
        '.'
    })
    // Join back the list items with a newline between each
    .join('\n');

  return list;
};

let str1 = "1. sun ; moon ; star ; god ; goddess";

let str2 = "sun; moon; star; god; goddess;";

let result = "1. sun.\n2. moon.\n3. star.\n4. god.\n5. goddess.";

console.log(formatList(str1), formatList(str1) === result);
console.log(formatList(str2), formatList(str2) === result);

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

2 Comments

Thanks but it would be great if we could put the . after each word.
Thank you so much. Since I've already accepted the answer but I really appreciate your help Thanks a lot. @giuseppedeponte
0

We can split on the regex /\s*;\s*/g, then handle the possibility that the number may already exist on the list item in the map function, as is the case in the first example.

let str1 = "1. sun ; a moon ; star ; god ; goddess ; ";

const result = str1.split(/\s*;\s*/g)
  .filter(Boolean)
  .map((e, i) => `${/^\d+\./.test(e) ? "" : i + 1 + ". "}${e}.`)
  .join("\n");

console.log(result);

6 Comments

Sorry I had to uncheck your answer since your code remove some text from the string example "a moon" converted to "2. moon." and "a god' to "3. god." and so on.
I updated the code to handle this shortly after you accepted the answer to be more robust to some of these issues, so try the latest version and let me know if it doesn't work for you.
I've checked your updated code. Your code is not removing the characters from the string but instead its creating a blank index number. I'll just update it now.
If the other answer worked for you, then all is well--I can delete this.
Ah, I see the problem. You have a delimiter at the end of the string, so that causes an empty string to be collected. See the update. At that point, though, it's pretty similar to the AC answer, so stick with that! Deleting shortly...
|

Your Answer

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