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"; }
24 Nov 2019 at 07:43 PM
0pnshow more
zeddzedd
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"; }
25 Nov 2019 at 07:17 PM
0pnshow more
TimTim
Share on FacebookShare on TwitterShare on LinkedInSend email
Follow us on Facebook & Twitter
2022 AnswerTabsTermsContact us