Regular expression for blood pressure

I have the following regex to validate blood pressure values ​​as systolic / diastolic:

\b[0-9]{1,3}\/[0-9]{1,3}\b

The expression works with the only drawback that it allows more than one consecutive slash (/) to be used. For example, he allows it 2/2/2

. I want it to only allow number format from 1 to 999 and forward slash and then number from 1 to 999. For example 83/23, 1/123, 999/999, 110/80, etc. Can someone help me?

The only other expression I found here is : ^\b(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)\\/(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)$

but it doesn't work.

By the way, I am using jquery.

Thank.

+3


source to share


3 answers


Use ^

and $

to match the beginning and end of a line:

^\d{1,3}\/\d{1,3}$

      



So you bind strings that match exactly from this form.

+8


source


Don't use word boundaries \b

because the forward slash is considered a word boundary.



Using ^

and / or $

is most likely your simplest solution. Unfortunately, if your input is part of a line or sentence, or occurs more than once in a line, etc., you think more about it.

+1


source


Expanding on Blender's answer, here's a simple check to check the BP value in the format: 120/80:

if(/^\d{1,3}\/\d{1,3}$/.test(120/80)) {
    console.log("BP Valid");
} else {
    console.log("Invalid BP");
}

      

0


source







All Articles