Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading