0

I have written some basic regex:

/[0123BCDER]/g

I would like to extract the bold numbers from the below lines. The regex i have written extracts the character however i would only like to extract the character if it is surrounded by white space. Any help would be much appreciated.

 721658A220421EE5867            AMBER YUR DE STE 30367887462580                  **1**                                                        00355132

 172638A220421ER3028            NIKITA YUAN         318058763400580                  **1**                                                        00355133

 982658A230421MC1234            SEAN D W MC100050420965155230421            **3**                  14032887609303                        00355134

Please note the character or digit will always be by itself.

Sean Bailey
  • 375
  • 3
  • 15
  • 33

1 Answers1

0

You are loking for something like this: /\s\d\s/g.

  • \s - match whitespace,
  • \d - match any digit,
  • /g - match all occurrences.

You can also replace \d with e.g. [0123BCDER] (your example) or [0-9A-Za-z] (all alphanumberic).

const input = `721658A220421EE5867            AMBER YUR DE STE 30367887462580                  1                                                       00355132

 172638A220421ER3028            NIKITA YUAN         318058763400580                  1                                                        00355133      _

 982658A230421MC1234            SEAN D W MC100050420965155230421            3                  14032887609303                        00355134
`

// with whitespaces
const res = input.match(/\s\d\s/g)
console.log(res)

// alphanumeric 
const res2 = input.match(/\s[A-Za-z0-9]\s/g)
console.log(res2)
ulou
  • 5,542
  • 5
  • 37
  • 47
  • Thank you so much for your help, this works if a digit is surrounded by white space however for instances where it is not a digit and one of the following characters this does not work [BCDER] the character will always be 1 digit/character long if that helps – Sean Bailey Apr 28 '21 at 12:02
  • Just replace `\d` with `[0123BCDER]` or whatever character you want. – ulou Apr 28 '21 at 12:04