0

I have a project and this project contains a text, and this text is in the form: patientId xxxxxx, which is in the form patient1075 0944713415. And I want to separate the text from the number, I tried to use several methods, but I could not separate the text from the number.

i need to pass 1075 to backend api, that is mean i need the number.

How can I do that?

                            const d = decrypt(t);
                            console.log('after decrypt: ', d); //patient1075
                            let numb: any = d.replace(/\D/g,'');
                            console.log('ccxcxccxcc: ', numb);
Hiba Youssef
  • 1,130
  • 1
  • 12
  • 34

2 Answers2

1

Try something like this

const num: number = parseInt("patient1075".match(/\d+/g));
// num[0] will be 1075

const text: string = "patient1075".match(/[a-zA-Z]+/g);
/* text[0] will be patient.
0

It seems like regex problem more than ts.

Try this

const d = 'patient1075' // your example

/** replace text to '' with regex,  
below regex means replace string that isn't number
*/
let numb:any = d.replace(/[^0-9]/g,'') 

console.log('numb',numb) // expect: 1075


(edit)

if the case your text have all sperated my blank.

you can try my code after split the text.

check this out

const d = 'patientId1075 0944713415'

// split the string by blank,
// the result is ['patientId1075', '0944713415']
// then pick the string that you need and remove text

let numb: any = d.split(" ")[0].replace(/[^0-9]/g, "");

console.log(numb) //expect: 1075
SpookyJelly
  • 105
  • 10