0

I need only line (1) and (3) by the keyword "a" "b" and "c" by regular expression and javascript.

Text is:

a+b+c (1)

test+a+test (2)

c+b+a(3)

test+b+test (4)

I found "(?=.*a).*b". But how can I do with more than 2 keywords like the example?

Bonn
  • 183
  • 14

1 Answers1

1

You can use positive lookahead assertions as in the following regex to match the required text:

/^(?=.*a)(?=.*b)(?=.*c)/

More details about lookahead and lookbehind can be found here: Regex lookahead, lookbehind and atomic groups

JavaScript Demo

var a = "a+b+c";
var b = "test+a+test";
var c = "c+b+a";
var d = "test+b+test";

var pattern = /^(?=.*a)(?=.*b)(?=.*c)/;

console.log(a + " ----- " + pattern.test(a));
console.log(b + " ----- " + pattern.test(b));
console.log(c + " ----- " + pattern.test(c));
console.log(d + " ----- " + pattern.test(d));
Community
  • 1
  • 1
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
  • I think you have added incorrect fiddle link. Also its better to use stack snippet as it makes you answer independent. – Rajesh Mar 03 '17 at 06:35