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

111
Views
Regular expression for cleaning up strings

[UPDATED]

In an effort to clean up an array of strings that contains people names as well as other titles or user IDs (if present), I need to remove any non-letter characters as well as all characters that are surrounded by parentheses, quotations marks, brackets etc.

For example ["Sean Jackson(he/him)", "Mack $ Adams"] needs to be changed to ["Sean Jackson", "Mack Adams"].

To achieve this I need to write a regular expression that can match such instances but unfortunately my regular expression does not work as intended.

let names = [
  'Sean Jackson (he/him)',
  'Steven Robinson',
  'Mack $ Adams',
  'Keira (12345) Nightly',
 ];

for (i in names) {
  names[i] = names[i].replace(/\W*\W/g, '');
 }

console.log(names);

The above code returns:

["SeanJacksonhehim", "StevenRobinson", "MackAdams", "Keira12345Nightly"]

Whereas what I want is:

["Sean Jackson", "Steven Robinson", "Mack Adams", "Keira Nightly"]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So after some digging and with helpful comments from the community I found the following working solution.

Since a space is counted as a non-alphanumerical character itself (\s is captured by non-alphanumerical regular expression \W), I had to specify that two consecutive non-alphanumerics need to be on the left side of the target string (hence using \W{2}).

And then on the right side of the target string there should be any number of characters except for space \S*.

let names = [
  'Sean Jackson (he/him)',
  'Steven Robinson',
  'Mack $ Adams',
  'Keira (12345) Nightly'
 ];

for (i in names) {
  names[i] = names[i].replace(/\W{2}\S*/g, ''); //removes the following patter: [any two non-alphanumerical character followed by any number of any characters except for space]
}
console.log(names);

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!