-2

I need my regular expression to execute wether the incoming value is a string or an integer. What I have only works with strings. My Javascript is the following:

var regExp = new RegExp(valueToMatch, 'gi');
item.ssn.match(regExp)

The valueToMatch could be either a character that looks like "1" or 1. So basically a string or an integer.

iChido
  • 3,972
  • 6
  • 26
  • 29

1 Answers1

1
var passVal = typeof(valueToMatch) === 'number'? valueToMatch.toString() : valueToMatch
var regExp = new RegExp(passVal , 'gi');
item.ssn.match(regExp)

You should check the value before passing through regex expression.

Han
  • 361
  • 1
  • 6
  • what is 'number'? – iChido Apr 24 '20 at 00:15
  • 1
    `number` is Number is a numeric data type which is `integer` in this case. Have a look : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof – Han Apr 24 '20 at 00:29