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

120
Views
how to slice string if numbers starts in javascript

I have strings like these

total sales 234 rs
total cost 651 rs

and I want to get only

end result should look like this

total sales
total cost

how can i get that please help thanks

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

0

Assuming you always want to cut it off just before the first number, you could find the index of the first number:

const index = 'total sales 234 rs'.search(/\d/);

And then just get the substring:

substring(0, index - 1);
about 4 years ago · Juan Pablo Isaza Report

0

You can do something like this:

let str = 'total sales 234 rs';
let result = str.split(' ').slice(0,2).join(' ');
about 4 years ago · Juan Pablo Isaza Report

0

Assuming that the string can contain more than two main words For eg Total goods cost 200 rs

then use the below function

function reducer(str) { // where str is the initial string
 const modifiedStr = str.split(' ').reduce((acc, substr) => {
  if(!parseInt(substr, 10) && substr !== 'rs') acc+=substr+' ';
  return acc;
 }, "");
 return modifiedStr.slice(0,modifiedStr.length - 2);
}
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!