简体   繁体   中英

Javascript: Remove words and words in brackets from array of strings

I am trying to remove unnecessary words from a string. Like for example, "Finely chopped" or removing words in brackets like "(Optional)". I have code that removes measurements but I am finding it difficult to get the desired output.

Input:

const Input = [
   'chopped kale, or to taste',
   'lemon, juiced',
   'small yellow squash, diced (Optional)'
]

I am just trying to make regex that looks for words in an array called unwanted words like 'to taste' and , 'diced' and removes them

Desired Output:

const output = [
   'chopped kale',
   'lemon',
   'small yellow squash'
]

This is my code:

import {measures} from './measures'
const regXMeasureAndMatter = RegExp('^(?<left>[^¼½¾\\d]+)*(?<count>¼|½|¾|\\d+\\/\\d+|\\d+)\\s*(?<unit>' + measures.join('|') + ')*(?<right>.*)');

function rearrangeMeasureAndMatter(match, left, count, unit, right) {
  left = (left || '' ).trim();
  right = (right || '' ).trim();
  return [
  
    count,
    (unit || ''),
    [left, right].join((left && right) || '')

  ].join(' ')
}

export function arrangeIngri(str) {
  return str
    .replace(regXMeasureAndMatter, rearrangeMeasureAndMatter)
    .replace(/\s+/g, ' ').trim()
}

// get proper ingris

export function createUnitCentricCapturingRegX(unitList) {
  const options = unitList
    .map(unit => escapeRegExpSearchString(unit))
    .join('|')
    .replace((/\\\.\\s\+/g), '\\\.\\s*');

  return RegExp('^(?<amount>.*?\\s*\\b(?:' + options + '))\\b\\s*(?<value>.*)$', 'i');
}
export const unitlessCapturingRegX = (/^(?<amount>¼|½|¾|\d+\/\d+|\d+)\s*(?<value>.*)$/);


export function collectNamedCaptureGroupData(collector, item) {
  item = item.trim();

  const { regXPrimary, regXSecondary, defaultKey, list } = collector;
  const result = regXPrimary.exec(item) || regXSecondary.exec(item);

  list.push(
    (result && result.groups && Object.assign({}, result.groups))
    || { [defaultKey]: item }
  );
  return collector;
}

function escapeRegExpSearchString(text) {

  return text
    .replace(/[-[\]{}()*+?.,\\^$|#]/g, '\\$&')
    .replace((/\s+/), '\\s+');
}
const splitMe = w => w.map(a => a.substr(0, a.lastIndexOf(",")))

const input = [
   'chopped kale, or to taste',
   'lemon, juiced',
   'small yellow squash, diced (Optional)'
]

code spits out as expected

const output = [
   'chopped kale',
   'lemon',
   'small yellow squash'
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM