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

147
Views
JQuery - remove text after character, keep string comma seperated

I have a string:

XXX - Test Text 1, OOO - Test Text 2, Dummy

What I want to do is return this string as:

XXX,OOO,Dummy

So I need to remove everything (and including the dash) but keep the string comma separated. This string could have up to say 10 variables.

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

0

This has nothing to do with jQuery, it is just native JavaScript. What you could do is split the string for every comma. Then split every part on the - character and join all the parts together:

parsedString = "XXX - Test Text 1, OOO - Test Text 2, Dummy".split(',')
  .map(part => part.split('-')[0].trim())
  .join(',')

console.log(parsedString);

about 4 years ago · Juan Pablo Isaza Report

0

1) You can first split the string which will returns an array of strings

str.split(",")

2) use reduce to only get the initial string of all strings in an array that is returned from the 1st step

3) then, join them with ,

const str = "XXX - Test Text 1, OOO - Test Text 2, Dummy";
const result = str
  .split(",")
  .reduce((acc, curr) => {
    let match = curr.match(/[a-z]+/i);
    if (match) acc.push(match[0]);
    return acc;
  }, [])
  .join(",");

console.log(result);

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!