0

I am using below JavaScript code to check whether the given REGEX is matching with String or not by using test method.

const str = 'AUGUST PRODUCT UPDATES AND NEWSLETTER';

const regex = new RegExp('.*INAWS.*|.*INAWS P4.*|.*INAWSP4.*|.*P4 INAWS.*|.*P4INAWS.*|.*INAWS P3.*|.*INAWSP3.*|.*P3 INAWS.*|.*P3INAWS.*|.*INAWS P2.*|.*INAWSP2.*|.*P2 INAWS.*|.*P2INAWS.*|.*INAWS P1.*|.*INAWSP1.*|.*P1 INAWS.*|.*P1INAWS.*|.*INSAP.*|.*INSAP P4.*|.*INSAPP4.*|.*P4 INSAP.*|.*P4INSAP.*|.*INSAP P3.*|.*INSAPP3.*|.*P3 INSAP.*|.*P3INSAP.*|.*INSAP P2.*|.*INSAPP2.*|.*P2 INSAP.*|.*P2INSAP.*|.*INSAP P1.*|.*INSAPP1.*|.*P1 INSAP.*|.*P1INSAP.*|.*INFW.*|.*INFW P4.*|.*INFWP4.*|.*P4 INFW.*|.*P4INFW.*|.*INFW P3.*|.*INFWP3.*|.*P3 INFW.*|.*P3INFW.*|.*INFW P2.*|.*INFWP2.*|.*P2 INFW.*|.*P2INFW.*|.*.*|.*INFW P1.*|.*INFWP1.*|.*P1 INFW.*|.*P1INFW.*|.*INSEC.*|.*INSEC P4.*|.*INSECP4.*|.*P4 INSEC.*|.*P4INSEC.*|.*INSEC P3.*|.*INSECP3.*|.*P3 INSEC.*|.*P3INSEC.*|.*INSEC P2.*|.*INSECP2.*|.*P2 INSEC.*|.*P2INSEC.*|.*INSEC P1.*|.*INSECP1.*|.*P1 INSEC.*|.*P1INSEC.*');

console.log(regex.test(str));

​The above code returns true ,

But i don't know which REGEX keyword is matching with above given String. I hope there is none of keyword is matched with given String, but don't know why it returns true.

Can anyone please help me to find out which REGEX is matching or I'm misunderstanding anything wrong since I'm newbie in Javascript?

4
  • You may have to check each pattern separately, e.g. in a large if-else. Commented Aug 26, 2021 at 6:51
  • 1
    it's .*.* that matches Commented Aug 26, 2021 at 6:53
  • Just put each single pattern into an array an loop through it to find out which is matching. Commented Aug 26, 2021 at 6:54
  • Hi @Bravo Thanks a lot. i start debugging by dividing REGEX as 4 part . findout matched REGEX in 3rdrow but not exact keyword, after seeing your comment, i can know that. Commented Aug 26, 2021 at 7:00

3 Answers 3

1

Given the original string you use to create the regex, you can use that string differently - like so

const str = 'AUGUST PRODUCT UPDATES AND NEWSLETTER';
const regexString = '.*INAWS.*|.*INAWS P4.*|.*INAWSP4.*|.*P4 INAWS.*|.*P4INAWS.*|.*INAWS P3.*|.*INAWSP3.*|.*P3 INAWS.*|.*P3INAWS.*|.*INAWS P2.*|.*INAWSP2.*|.*P2 INAWS.*|.*P2INAWS.*|.*INAWS P1.*|.*INAWSP1.*|.*P1 INAWS.*|.*P1INAWS.*|.*INSAP.*|.*INSAP P4.*|.*INSAPP4.*|.*P4 INSAP.*|.*P4INSAP.*|.*INSAP P3.*|.*INSAPP3.*|.*P3 INSAP.*|.*P3INSAP.*|.*INSAP P2.*|.*INSAPP2.*|.*P2 INSAP.*|.*P2INSAP.*|.*INSAP P1.*|.*INSAPP1.*|.*P1 INSAP.*|.*P1INSAP.*|.*INFW.*|.*INFW P4.*|.*INFWP4.*|.*P4 INFW.*|.*P4INFW.*|.*INFW P3.*|.*INFWP3.*|.*P3 INFW.*|.*P3INFW.*|.*INFW P2.*|.*INFWP2.*|.*P2 INFW.*|.*P2INFW.*|.*.*|.*INFW P1.*|.*INFWP1.*|.*P1 INFW.*|.*P1INFW.*|.*INSEC.*|.*INSEC P4.*|.*INSECP4.*|.*P4 INSEC.*|.*P4INSEC.*|.*INSEC P3.*|.*INSECP3.*|.*P3 INSEC.*|.*P3INSEC.*|.*INSEC P2.*|.*INSECP2.*|.*P2 INSEC.*|.*P2INSEC.*|.*INSEC P1.*|.*INSECP1.*|.*P1 INSEC.*|.*P1INSEC.*';

const patterns = regexString.split('|');
const regex = new RegExp(`(${patterns.join(')|(')})`);
const matches = str.match(regex);
if (matches) {
    const listOfMatches = Object.entries(matches.slice(1)).filter(([index, match]) => match).map(([index, match]) => [patterns[index], match]);
    console.log(listOfMatches);
}

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

1 Comment

Hi @Bravo, Thanks for providing solution and for your time.
0

If you want to check which word in str matches the regex pattern, you can break up the str into words in an array.

const str = 'AUGUST PRODUCT UPDATES AND NEWSLETTER';
let wordList = str.split(" ");

This way, you can run RegExp.prototype.test() method on each word and see which word matches the regex pattern.

const regex = new RegExp(...);
const str = 'AUGUST PRODUCT UPDATES AND NEWSLETTER';

let result = [];
let wordList = str.split(" ");

wordList.map(word => {
    if(regex.test(word)) {
        result.push(word);
    }
});
console.log(result); // matched words will be logged in array format

2 Comments

Hi @Kouta Nakano, Thanks for your solution. i want which pattern is matched with given String. your code will return all matched keyword. anyway Thanks for your suggestion
I see. Good luck!
0

To match parts of your regex you need to encapsulate each pattern with braces. This is called "groups" in regex. Than use the match() function to get more information. You'll get an array as the result. The first item is always the entire string that matched. All following items are matches for each group. Have a look at this code:

const str = 'AUGUST PRODUCT UPDATES AND NEWSLETTER';

const regex = '/(.*INAWS.*)|(.*INAWS P4.*)|(.*INAWSP4.*)|(.*P4 INAWS.*)|(.*P4INAWS.*)|(.*INAWS P3.*)|(.*INAWSP3.*)|(.*P3 INAWS.*)|(.*P3INAWS.*)|(.*INAWS P2.*)|(.*INAWSP2.*)|(.*P2 INAWS.*)|(.*P2INAWS.*)|(.*INAWS P1.*)|(.*INAWSP1.*)|(.*P1 INAWS.*)|(.*P1INAWS.*)|(.*INSAP.*)|(.*INSAP P4.*)|(.*INSAPP4.*)|(.*P4 INSAP.*)|(.*P4INSAP.*)|(.*INSAP P3.*)|(.*INSAPP3.*)|(.*P3 INSAP.*)|(.*P3INSAP.*)|(.*INSAP P2.*)|(.*INSAPP2.*)|(.*P2 INSAP.*)|(.*P2INSAP.*)|(.*INSAP P1.*)|(.*INSAPP1.*)|(.*P1 INSAP.*)|(.*P1INSAP.*)|(.*INFW.*)|(.*INFW P4.*)|(.*INFWP4.*)|(.*P4 INFW.*)|(.*P4INFW.*)|(.*INFW P3.*)|(.*INFWP3.*)|(.*P3 INFW.*)|(.*P3INFW.*)|(.*INFW P2.*)|(.*INFWP2.*)|(.*P2 INFW.*)|(.*P2INFW.*)|(.*.*)|(.*INFW P1.*)|(.*INFWP1.*)|(.*P1 INFW.*)|(.*P1INFW.*)|(.*INSEC.*)|(.*INSEC P4.*)|(.*INSECP4.*)|(.*P4 INSEC.*)|(.*P4INSEC.*)|(.*INSEC P3.*)|(.*INSECP3.*)|(.*P3 INSEC.*)|(.*P3INSEC.*)|(.*INSEC P2.*)|(.*INSECP2.*)|(.*P2 INSEC.*)|(.*P2INSEC.*)|(.*INSEC P1.*)|(.*INSECP1.*)|(.*P1 INSEC.*)|(.*P1INSEC.*)/';

console.log(str.match(regex));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.