Home > Software design >  Substrings or split methods in javascript
Substrings or split methods in javascript

Time:03-09

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.

CodePudding user response:

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);

CodePudding user response:

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)"));

  • Related