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

188
Views
JS Pig poem matching substring with Regexp

I have a one long string with sentences, every sentence contains a number. Every sentence is separated by /X. Having a given number, how can I match and extract only the one sentence which contains that number?

"\X The animal 0000 I really dig \X Above all others is the pig. 222 \X Pigs are noble. Pigs are clever 3333, \X Pigs are 5555 courteous.\X However, Now and then, to break this 6666 rule, \X One meets 7777 a pig who is a fool. \X"

For example, I want to extract the sentence with the number 5555, to get this: " Pigs are 5555 courteous."

How to do this with JS regexp? My code matches the whole text:

str.match(/\\X.*5555.*\\X/);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to make sure to exclude in .* the case when it would match \\X.

This worked for me:

/[^\\X]*5555[^\\X]*/g

Use this if you want to exclude the first space of your sentence, however it requires support for Lookbehind:

/(?<=\\X )[^\\X]*5555[^\\X]*/g
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!