1

I have a simple problem that I think I need help with. So, I have a function that accepts a string in this format

"1. Crowe, Velvet (LoC), 2. Hume, Eleanor (Ext), 4. Shigure, Rokurou (DmN), 10. Mayvin, Magilou (MgC)" without the quotation marks.

Basically, a list of person's names with rank number.

What I want is to split them such that I will get the ff result:

[
   "1. Crowe, Velvet (LoC)", 
   "2. Hume, Eleanor (Ext)", 
   "4. Shigure, Rokurou (DmN)",
   "10. Mayvin, Magilou (MgC)"
]

Is there a way to do that? I used split() method but it splits the string every time it sees an occurrence of the comma.

4
  • Is the format for each item always <number>. <text>, <text> (<text>)? Commented Mar 9, 2022 at 11:20
  • @NickParsons, yes. It's always like that Commented Mar 9, 2022 at 11:21
  • hacky and probably not best. split by ("),") and map result array and add ")" to each element. "1. Crowe, Velvet (LoC), 2. Hume, Eleanor (Ext), 4. Shigure, Rokurou (DmN), 10. Mayvin, Magilou (MgC)".split("),").map(el => el+")") Commented Mar 9, 2022 at 11:22
  • @Hansel actually no the last element breaks Commented Mar 9, 2022 at 11:28

2 Answers 2

2

You could split using a regular expression /(?<=\)),\s/:

const str = "1. Crowe, Velvet (LoC), 2. Hume, Eleanor (Ext), 4. Shigure, Rokurou (DmN), 10. Mayvin, Magilou (MgC)";
const res = str.split(/(?<=\)),\s/);
console.log(res);

This will split on ), as suggested by cmgchess in the comments. The issue with splitting based on a string is that it removes some of the characters you want to keep. Instead, by using a regular expression, you can use (?<=\)) (here ?<= is called a positive lookbehind) to match and keep the ) in the resulting split element. The ,\s then splits based on a comma followed by a space (\s).

You could also use the following regex with .match() which is a little more robust, but may need to be updated depending on the text that can come after your number/rank:

/\d+\.\s\w+,\s\w+\s\(\w+\)/g

The above performs:

  • \d+\.: Will match one or more (+) digits followed by a dot
  • \s: Match a whitespace character
  • \w+,: Match one of more (+) word-characters (\w) followed by a comma ,
  • \s: Matches a space
  • \(\w+\): Match word characters surrounded by parentheses ( )
  • /g: Global flag to match the sequences for all occurrences in the string

const str = "1. Crowe, Velvet (LoC), 2. Hume, Eleanor (Ext), 4. Shigure, Rokurou (DmN), 10. Mayvin, Magilou (MgC)";
const res = str.match(/\d+\.\s\w+,\s\w+\s\(\w+\)/g);
console.log(res);

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

Comments

0

Just a little improvement of @cmgchess's answer (add trim method to remove unnecessary spaces):

const getUsers = (str) => {
    const users = str.split('),').map(x => x.trim() + ')');
    users[users.length - 1] = users[users.length - 1].replace('))', ')');

    return users;
}

console.log(getUsers("1. Crowe, Velvet (LoC), 2. Hume, Eleanor (Ext), 4. Shigure, Rokurou (DmN), 10. Mayvin, Magilou (MgC)"));

3 Comments

actually my answer is not correct check the last element. maybe have to apply regex
Ah, there is an extra ")" at the last element. That was what @cmgchess meant by "the last element breaks".
Sorry guys, now I replace extra )

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.