Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to extract each alphanumeric pattern

I have a string like following

19990101 - John DoeLorem ipsum dolor sit amet 19990102 - Elton Johnconsectetur adipiscing elit

How can I write a regex that would give me these two separate strings

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

19990101 - John DoeLorem ipsum dolor sit amet

19990102 - Elton Johnconsectetur adipiscing elit

The regex I wrote works up to this

/\d+ -/gm

Image

But I don’t know how can I include the alphabets there as well

Image2

>Solution :

You can use

const text = '19990101 - John DoeLorem ipsum dolor sit amet 19990102 - Elton Johnconsectetur adipiscing elit';
console.log(text.match(/\d+\s+-[A-Za-z0-9\s]*[A-Za-z]/g))
console.log(text.split(/(?!^)\s+(?=\d+\s+-)/))

The text.match(/\d+\s+-[A-Za-z0-9\s]*[A-Za-z]/g) approach is extracting the alphanumeric/whitespace chars after \d+\s+- pattern. Details:

  • \d+ – one or more digits
  • \s+ – one or more whitespaces
  • - – a hyphen
  • [A-Za-z0-9\s]* – zero or more alphanumeric or whitespace chars
  • [A-Za-z] – a letter

The text.split(/(?!^)\s+(?=\d+\s+-)/) splitting approach breaks the string with one or more whitespaces before one or more digits + one or more whitespaces + -:

  • (?!^) – not at the start of string
  • \s+ – one or more whitespaces
  • (?=\d+\s+-) – a positive lookahead that matches a location that is immediately followed with one or more digits + one or more whitespaces + -.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading