1

i have this list:

tests.skills
tests.instructions
tests.something

I want to test if the given string include any of the last two but not the first one, so i tried this:

 var str = 'tests.instructions';
 if( /tests\.[^skills].+/ig.test(str) ) {
      console.log(1);
  }

But it does not work, how can i test this?

0

2 Answers 2

2
/tests\.(?!skills).+/

The set negation [^x] will try to match a character that is not x. What [^skills] actually means is match a single character, which is not s or k or i or l or l or s.

Instead a negative lookahead (?!sequence) will do the job.

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

2 Comments

You can explain what your regex do?
@user233232, I guess you already know everything except for the (?!) part. It is called a negative lookahead. Basically, what it does is matches an empty string if what follows in the current position is not skills and fails the entire match otherwise.
1

You can also try:

tests\.(?:instructions|something)

It means: search for tests. text which is followed by instructions or something.

Regex live here.

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.