1

So I know how to get a substring from 2 characters using index or split method. But I'm stuck in a scenario of lots of string with similar names such as:

   "2020-12-09-name_of_this_mission_num1_mission1_fileName_something"
   "2020-12-09-name_of_this_mission_num1_mission12_fileName_something"
   "2020-12-09-name_of_this_mission_num23_mission1_fileName_something_else"

Like I am stuck on how to extract just the "mission#" part, because sometimes the names can be different, so the length is different, and sometimes the names are the same, same as the fileName. I also thought about using the index of "_", but there are multiple "_" and they might end up in different index if the name is different.

Could anyone give me some hint on this?

2
  • So your desired outputs are mission1, mission12, and mission1 respectively? Commented Dec 10, 2020 at 1:11
  • Yes, that is what I want to extract Commented Dec 10, 2020 at 1:13

3 Answers 3

2

If the structure of the strings are always the same - and you want the second instance of 'mision' - then split the full string on the text of 'mission'.

This will yield an array with three portions - ["2020-12-09-name_of_this_", "num1", "1_fileName_something"])

Then get the last item in this portions array and grab the number from the start of the resultant string.

Then you can prefix it with the 'mission' that you removed, push it into an array and you have a array of of missions.

If your initial string does not contain a two instances of 'mission' then you can set it to return the 2nd not 3rd portion as I have doen with 'mission2'.

const  missions =  [
  "2020-12-09-name_of_this_mission_num1_mission1_fileName_something",
  "2020-12-09-name_of_this_mission_num1_mission12_fileName_something",
  "2020-12-09-name_of_this_mission_num23_mission1_fileName_something_else",
  "2020-12-09-name_of_this_mission2_fileName_something_else"
]


let missionsArr = [];

missions.forEach(function(mission) {
  const missionPortions = mission.split('mission');
  let index; 
  
  missionPortions.length == 2
    ? index = 1
    : index = 2
    
  missionsArr.push('mission' + parseInt(missionPortions[index]))
})


console.log(missionsArr); //gives ["mission1","mission12", "mission1", "mission2"];

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

1 Comment

Thanks a lot, all you guys' answers are great help!!
2

A simple regex match function. Note that 'match' outputs an array, so push match[0]:

const  missions =  [
  "2020-12-09-name_of_this_mission_num1_mission1_fileName_something",
  "2020-12-09-name_of_this_mission_num1_mission12_fileName_something",
  "2020-12-09-name_of_this_mission_num23_mission1_fileName_something_else"
]

let Arr = [];

missions.forEach(function(mission) {
  const missionID = mission.match(/mission\d+/);
  Arr.push(missionID[0]);
})

console.log(Arr);

1 Comment

I have figured it out, thanks for the help!!
1

Easiest way to just get the mission##, assuming # is a variable number of digits, is by using regex.

The base regex would be /mission\d+/ which matches the string "mission" followed by at least one number.

Assuming you have your input as:

const missionsTexts = [
  "2020-12-09-name_of_this_mission_num1_mission1_fileName_something",
  "2020-12-09-name_of_this_mission_num1_mission12_fileName_something",
  "2020-12-09-name_of_this_mission_num23_mission1_fileName_something_else"
];

You can transform them into an array of just mission# with the following algorithm:

const missions = missionsTexts.map(missionText => missionText.match(/mission\d+/g)[0]);

Note that this assumes there's only one mission# per missionText. The g modifier is used to make sure the regex doesn't create a match after the first digit it finds.

2 Comments

Oh, I was thinking about index of first and last part, but never thought of doing something with the part that we want. This is great, thanks a lot.
No problem! Array operations and regex are usually pretty powerful tools

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.