Convert number to text string for match() regex - Javascript

I need to check if the number contain specific value, using javascript match() function and regex pattern. Value numbervalue is the result of calculation. For some reason, the condition below doesn't work. Calculated result probably doesn't look like correct input for match() function. Do you have any suggestions how can I convert the number value to text string, that will work with the condition ?

var numbervalue = a / b;
if( numbervalue.match(/\1.5/) ){ var check = "Ok"; } else{ var check = "Not Ok"; }
0
give a positive ratinggive a negative rating
24 Nov 2019 at 07:43 PM
Hi,

To convert a number to a string, that should work with javascript match() and regex pattern, you have to use toString() function.

The source should look like:

var numbervalue = a / b;
var stringvalue = numbervalue.toString();
if( stringvalue.match(/\1.5/) ){ var check = "Ok"; } else{ var check = "Not Ok"; }
0
give a positive ratinggive a negative rating
25 Nov 2019 at 07:17 PM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
Follow us on Facebook
2022 AnswerTabsTermsContact us