Home > Back-end >  Summarize the text and stick all the letters together
Summarize the text and stick all the letters together

Time:10-13

I have a text and want to summarize it , i want change this array :

Array 1

[
  'CALX',  '11.10',  '21',
  '01',    '08',     'EGLD',
  'USDT',  'LDFDFC', 'ZONE',
  '238.5', '233',    'LEVERAGE',
  '3',     'X',      'TARGET',
  '1',     '243.9',  'TARGET',
  '2',     '248',    'TARGET',
  '3',     '254',    'TARGET',
  '4',     '260',    'H',
  'GD',    'S',      'AFCA'
]

to this : Array 2

[
  'CALX',  '11.10',  '21',
  '01',    '08',     'EGLDUSDTLDFDFCZONE',
  '238.5', '233',    'LEVERAGE',
  '3',     'XTARGET',      
  '1',     '243.9',  'TARGET',
  '2',     '248',    'TARGET',
  '3',     '254',    'TARGET',
  '4',     '260',    'HGDSAFCA',
]

as you can see , I want all the letters to stick together until they reach a number,and each number should be in an element of the array

This is the code that can be used to convert text to an Array1

  const input = 'CALX, [11.10.21 01:08]  $EGLD/USDT  #Ldfdfc  zone : 238.5 - 233  "LEVERAGE" : 3x TARGET1 : 243.9 TARGET 2 : 248 TARGET 3 : 254 TARGET 4 : 260 h.gd.s afca. `~!@#$%^&*()_- =-/?><'

  const text = text.toUpperCase().match(/[a-z] |\d (?:\.\d )?/gi);

so how can i change the Array1 to Array2?

sorry for my English and thank you for your help.

CodePudding user response:

Based on the initial string, to get the desired array as output you don't have to convert it to an array to process it again.

You can use a pattern similar like the one that you tried with an alternation | but instead of matching [a-z] you can capture 1 or more non digits using (\D ) in a group.

Then in the callback of replace, you can remove the unwanted characters if there is a match for the group 1. The unwanted characters are [\W_] or one more non word chars including the underscore.

If there is no group, you can return the match (the digits) between delimiters, where you can split on the delimiters afterwards to create the final array.

const input = 'CALX, [11.10.21 01:08]  $EGLD/USDT  #Ldfdfc  zone : 238.5 - 233  "LEVERAGE" : 3x TARGET1 : 243.9 TARGET 2 : 248 TARGET 3 : 254 TARGET 4 : 260 h.gd.s afca. `~!@#$%^&*()_- =-/?><'
text = input
  .toUpperCase()
  .replace(/\d (?:\.\d )?|(\D )/g,
    (m, g1) => g1 ? g1.replace(/[\W_] /g, '') : `#${m}#`
  );
console.log(text.split(/# /));

CodePudding user response:

One of the solution could look like this:

let arr = [
    'CALX',  '11.10',  '21',
    '01',    '08',     'EGLD',
    'USDT',  'LDFDFC', 'ZONE',
    '238.5', '233',    'LEVERAGE',
    '3',     'X',      'TARGET',
    '1',     '243.9',  'TARGET',
    '2',     '248',    'TARGET',
    '3',     '254',    'TARGET',
    '4',     '260',    'H',
    'GD',    'S',      'AFCA'
]

function handleArray(a) {
    let result = [];
    let stringItem = '';

    a.forEach((el) => {
      // If number then check if we have previous string and push 
      // it to the result. 
      // Also push number as next element
        if (/\d/.test(el)) {
            if (stringItem) {
                result.push(stringItem);
                
                // Clear string variable
                stringItem = '';
            }
            result.push(el)
        } else {
            // Concat ongoing string, don't push to result
            stringItem  = el;
        }
    })

    return result;
}

console.log(handleArray(arr))

  • Related