Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex for multiple option

I need to write a regex expression to use with JS .match() function. The objective is to check a string with multiple alternatives. For example, I want to return true in below code if mystr contains word1 or word2 or word3

mystr1 = "this_is_my_test string_containing_word2_where_i_will_perform_search";
mystr2 = "this_is_my_test string_where_i_will_perform_search";
myregex = xxxxxx; // I want help regarding this line so that 
if(mystr1.match(myregex)) return true; //should return true
if(mystr2.match(myregex)) return true; //should NOT return true

Any help please?

like image 493
abdfahim Avatar asked Jul 17 '26 19:07

abdfahim


1 Answers

So use the OR | in your RegEx:

myregex = /word1|word2|word3/;
like image 184
Konstantin Dinev Avatar answered Jul 19 '26 10:07

Konstantin Dinev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!