Check if a variable contains any of the flowing numbers

Hello Experts,

In the code below I am getting the id attribute of the element  parent.
Then I use substring to "clean" the id attribute.
Since I use an each function of all the id's i need to check if the variable contains any of the following numbers.
e.g. 07, 41, 21.

var unitId = $(this).parent().attr("id").substring(4);   // Original id is something like "unit07" => I am getting "07".

// check if "unitId" contains any of the following numbers 07, 41, 21 and etc...

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

// remove non-digits (\D)
var unitId = $(this).parent().attr("id").replace( /\D/g,'');

if( /^(07|41|21)$/.test(unitId) )
  alert( "Matched " + unitId);

Open in new window

RefaelAuthor Commented:
Hi hielo

i did it like so.... seem to work... what do you think

if ((unitId).indexOf("11" || "21" || "31" || "41") !== -1) {

  // any of these following numbers exists

Open in new window

That will not work. Try it with unitId="21" and see.  The proper way to do that with indexOf is to test each case individually:
if ( (unitId).indexOf("11")!==-1 || (unitId).indexOf("21")!==-1 || (unitId).indexOf("31")!==-1 || (unitId).indexOf("41") !== -1) {

  // any of these following numbers exists

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RefaelAuthor Commented:
Sorry hielo i was away. Your solution is the best and it is already working on the client's website :-) Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.