Много параметров функции + регулярные выражения

Создайте функцию "parts", которая принимает несколько параметров. Каждый параметр - это группа предложений. Эта функция должна извлекать подстроку от знака «:» (двоеточие) до знака «.» (Точка) каждого параметра. и вернуть массив этих подстрок. Используйте Function Definition Expressions. Не понимаю, что делаю не так.

const param1 = 'This is the first sentence. This is a sentence with a list of items: cherries, oranges, apples, bananas.';
const param2 = 'This is the second sentence. This is a sentence with a list of items: red, blue, yellow, black.';

let result = function parts(...param) {
  param.match(/:(.*?)\./gmi);

};

parts(param1, param2);


Ответы (2 шт):

Автор решения: entithat

У вашем коде функция parts будет видна только внутри этой же функции. Обычно функции написаны как Function Expression анонимны (просто есть переменная, которая ссылается на них).

function [name]([param1[, param2[, ..., paramN]]]) {
   statements
}

name - The function name. Can be omitted, in which case the function is anonymous. The name is only local to the function body.

const param1 = 'This is the first sentence. This is a sentence with a list of items: cherries, oranges, apples, bananas.';
const param2 = 'This is the second sentence. This is a sentence with a list of items: red, blue, yellow, black.';

const parts = function (...param) {
  return param.map(e => e.match(/:(.*?)\./gmi));
};

console.log(parts(param1, param2));

→ Ссылка
Автор решения: vsemozhebuty

Попробуйте так, и проанализируйте разницу:

const param1 = 'This is the first sentence. This is a sentence with a list of items: cherries, oranges, apples, bananas.';
const param2 = 'This is the second sentence. This is a sentence with a list of items: red, blue, yellow, black.';

const parts = function (...param) {
  return param.map(str => str.match(/:(.*?)\./)[1]);
};

const result = parts(param1, param2);
console.log(result);

→ Ссылка