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"; }
posted on 24.11.2019 at 19:43
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"; }
posted on 25.11.2019 at 19:17
0pnshow more
TimTim
share on facebookshare on twitter
2019 AnswerTabsTermsContact us
This site uses cookies to improve your experience, for analytics and ads. By continuing to browse this site you agree to our use of cookies.