Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
Substrings or split methods in javascript

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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!