-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.

4
  • 1
    Soooo........ what's valueToMatch? Commented Apr 23, 2020 at 22:56
  • I got the Integer part but what do you mean if it is a string? Do you want to check the type of data? Commented Apr 23, 2020 at 22:56
  • I've made an update with some info. Commented Apr 24, 2020 at 0:15
  • You may simply use regExp.test(s), or regExp.test("" + s) - the string will get coerced to a string automatically. Commented Apr 24, 2020 at 8:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

what is 'number'?
number is Number is a numeric data type which is integer in this case. Have a look : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.