Click here to Skip to main content
15,867,997 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Use regex to return true if the input string in the following list.

matches
jacket
blanket
compass
pot
hat
screwdriver
fork
book
penknife

False if it is in the following list

smartphone
sword
axe
radio
walkman
tv
camera
boombox
skateboard
smartwatch

Regex pattern length should not be more than 25 characters.

Regex should fit in the code written below.

Constraints

All string will be from one of two lists.

code snippet:

Regex_Test tester = new Regex_Tester(); tester.checker(______); // fill here

What I have tried:

I HAVE TRIED USING LAST ALPHABET AND IT DIDN'T WORK
Posted
Updated 4-Aug-21 22:15pm

1 solution

The way this question is phrased makes it sound like homework, so it is your responsibility to put together the solution. We can point you in the right direction but you need to solve the problem yourself so you can learn. That being said, in Javascript you probably want to use regular expressions:

RegExp.prototype.test() - JavaScript | MDN[^]
Regular expressions - JavaScript | MDN[^]

A quick example:
JavaScript
const check     = /^[0-9]+$/g;
const notNumber = check.test('abc123'); // false
const yesNumber = check.test('12345');  // true
 
Share this answer
 
Comments
Ashwin Shankar 5-Aug-21 4:57am    
I appreciate your concern, but i am looking for a solution to this question, since i have tried it earlier and didn't get it.
Chris Copeland 5-Aug-21 6:02am    
You need to do some research into regular expressions and understand how they work. A good starting point would be to look at "anchors" (which can be used to detect the start and end of a string), and "alternation" (which can be used to check for combinations of options)

/^hello/g checks that the string starts with the word "hello", while /world$/g checks that the string ends with the word "world"

/(hello|world)/g checks that the string contains either the word "hello" or "world"
Richard Deeming 5-Aug-21 6:14am    
And as Chris explained, it's your homework, so it's your responsibility to find the solution.

Nobody here is going to do your homework for you.

If you genuinely don't know where to start, then talk to your teacher.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900