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 match for blood pressure value in JavaScript

I have some strings containing blood pressure data, the numbers before the / indicate the higher/upper end of the blood pressure (systolic), and the numbers after the / indicate the lower end of the blood pressure (diastolic).

The strings are like this

let str1 = "170/80"; // Should Pass
let str2 = "90/60"; // Should Pass
let str3 = "140/90"; // Should Pass
let str4 = "should not/pass"; // Shouldn't Pass

I have a regex that tries to make sure that these strings indeed are blood pressure readings, i.e., making sure that the values are all numbers and in between them is a / but it doesn’t seem to work (It’s not matching even with the supposedly correct 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

My regex is: ((\d)/(\d))

Can anyone tell me why it’s not working? and what should be the correct regex for this?

The language as obvious from the title is JavaScript.

>Solution :

The issue in your regex is that you’re not escaping your slash \, but also you are matching single digits \d. In order to solve these problems, you may want a regex like this:

(\d+)\/(\d+)

Check the demo here.

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