Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
Summarize the text and stick all the letters together

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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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(/#+/));

about 4 years ago · Juan Pablo Isaza Report

0

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))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!