Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
86
rated 0 times [  88] [ 2]  / answers: 1 / hits: 20093  / 12 Years ago, fri, january 11, 2013, 12:00:00

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?


More From » regex

 Answers
25

So use the OR | in your RegEx:



myregex = /word1|word2|word3/;

[#80934] Thursday, January 10, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
coleman

Total Points: 518
Total Questions: 81
Total Answers: 96

Location: Aland Islands
Member since Wed, Nov 17, 2021
3 Years ago
;