I have a multiline text, e.g.
JavaScript
x
4
1
word1 in line1
2
word2 in line2
3
word3 in line 3
4
I need to see if two words present in whole text (AND operator). I tried something like:
JavaScript
1
2
1
/^.*(?=.*word1)(?=.*word3).*$/gm
2
Advertisement
Answer
You can try this regex:
JavaScript
1
2
1
/^(?=[sS]*bword1b)(?=[sS]*bword3b)/
2
[sS]
matches literally everything, encluding line wrapsb
is the word bound, soword1
count butsword1
does not.
And since you treat all the lines as a whole, you dont need m
flag
Also you’re only testing the text, you don’t need g
flag either
JavaScript
1
7
1
const text = `word1 in line1
2
word2 in line2
3
word3 in line 3`;
4
5
const regex = /^(?=[sS]*bword1b)(?=[sS]*bword3b)/;
6
7
console.log(regex.test(text));