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

172
Views
How to find the string contain number with suffix 'xyz' using javascript regex?

Having a array of string, I want to filter(identify) the strings contain a number followed by 'xyz'.

Input: ['Carrot 22xyz', 'Mango', 'Banana 8xyz each', 'Kiwi']
Output:  ['Carrot 22xyz', 'Banana 8xyz each']
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use array's filter using following regex

/\dxyz/

enter image description here

const arr = ["Carrot 22xyz", "Mango", "Banana 8xyz each", "Kiwi"];
const result = arr.filter((s) => s.match(/\dxyz/));
console.log(result);

Note: This will also filter out the result if a number followed by xyz that is a part of a string like "apple4xyz", "mango69xyzfast",

If you only want to filter out that is not part of a substring then you can do as:

/\b\d+xyz\b/

enter image description here

const arr = [
  "Carrot 22xyz",
  "Mango",
  "Banana 8xyz each",
  "Kiwi",
  "apple4xyz",
  "mango69xyzfast",
];
const result = arr.filter((s) => s.match(/\b\d+xyz\b/));
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

The regex you need is /\dxyz/. Here is how you use it

const input = ['Carrot 22xyz', 'Mango', 'Banana 8xyz each', 'Kiwi']

console.log(input.filter(mabFruit => /\dxyz/.test(mabFruit)));

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!