jquery/javascript check string for multiple substrings
up vote
4
down vote
favorite
I need to check if a string has one of three substrings, and if yes, to implement a function. I know I can check for one substring using if (str.indexOf("term1") >= 0) but is there a way to check for multiple substrings short of using several instances of this code?
TIA
javascript jquery string search
add a comment |
up vote
4
down vote
favorite
I need to check if a string has one of three substrings, and if yes, to implement a function. I know I can check for one substring using if (str.indexOf("term1") >= 0) but is there a way to check for multiple substrings short of using several instances of this code?
TIA
javascript jquery string search
Put it in a loop?
– musefan
Mar 4 '13 at 12:46
1
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0)How difficult could it be?
– Alvin Wong
Mar 4 '13 at 12:48
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I need to check if a string has one of three substrings, and if yes, to implement a function. I know I can check for one substring using if (str.indexOf("term1") >= 0) but is there a way to check for multiple substrings short of using several instances of this code?
TIA
javascript jquery string search
I need to check if a string has one of three substrings, and if yes, to implement a function. I know I can check for one substring using if (str.indexOf("term1") >= 0) but is there a way to check for multiple substrings short of using several instances of this code?
TIA
javascript jquery string search
javascript jquery string search
edited Nov 8 at 8:54
Dem Pilafian
2,73612548
2,73612548
asked Mar 4 '13 at 12:46
Phil
67451430
67451430
Put it in a loop?
– musefan
Mar 4 '13 at 12:46
1
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0)How difficult could it be?
– Alvin Wong
Mar 4 '13 at 12:48
add a comment |
Put it in a loop?
– musefan
Mar 4 '13 at 12:46
1
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0)How difficult could it be?
– Alvin Wong
Mar 4 '13 at 12:48
Put it in a loop?
– musefan
Mar 4 '13 at 12:46
Put it in a loop?
– musefan
Mar 4 '13 at 12:46
1
1
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) How difficult could it be?– Alvin Wong
Mar 4 '13 at 12:48
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) How difficult could it be?– Alvin Wong
Mar 4 '13 at 12:48
add a comment |
5 Answers
5
active
oldest
votes
up vote
7
down vote
You could use a loop. Maybe even create a helper function like so:
function ContainsAny(str, items){
for(var i in items){
var item = items[i];
if (str.indexOf(item) > -1){
return true;
}
}
return false;
}
Which you can then call like so:
if(ContainsAny(str, ["term1", "term2", "term3"])){
//do something
}
From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
– Anthony Grist
Mar 4 '13 at 12:56
@AnthonyGrist: Yeah I seen that already and just changed it
– musefan
Mar 4 '13 at 12:57
add a comment |
up vote
7
down vote
if (/term1|term2|term3/.test("your string")) {
//youre code
}
1
Explanation of downvote would be nice.
– elrado
Mar 4 '13 at 12:56
You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
– Ash Burlaczenko
Mar 4 '13 at 13:01
4
Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
– elrado
Mar 4 '13 at 13:06
explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
– guradio
Nov 8 at 8:58
add a comment |
up vote
3
down vote
Maybe this:
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0)
{
//your code
}
+1 for the clear simple solution !
– Hussein Nazzal
Mar 4 '13 at 12:51
1
Not ideal for an unknown number of terms though
– musefan
Mar 4 '13 at 12:55
1
I agree, but he asked for 3 substrings.
– freshbm
Mar 4 '13 at 13:00
2
but he askedshort of using several instances of this code
– Arun P Johny
Mar 4 '13 at 13:16
add a comment |
up vote
2
down vote
You can do something like
function isSubStringPresent(str){
for(var i = 1; i < arguments.length; i++){
if(str.indexOf(arguments[i]) > -1){
return true;
}
}
return false;
}
isSubStringPresent('mystring', 'term1', 'term2', ...)
Never seen this before, using extra arguments. Can you point me to the docs?
– Ash Burlaczenko
Mar 4 '13 at 12:53
Interesting, I never knew aboutargumentsbefore... though I assume this will be vulnerable to redefinition?
– musefan
Mar 4 '13 at 12:54
every javascript method get a hidden parameter calledargumentswhich is an array like structure with all the parameters passed to the method. It is like variable arguments in java
– Arun P Johny
Mar 4 '13 at 12:55
no, it is as per standard ECMA-262
– Arun P Johny
Mar 4 '13 at 12:58
2
On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
– Ash Burlaczenko
Mar 4 '13 at 13:06
|
show 2 more comments
up vote
0
down vote
The .map() function can be used to convert an array of terms into an array of booleans indicating if each term is found. Then check if any of the booleans are true.
Given an array of terms:
const terms = ['term1', 'term2', 'term3'];
This line of code will return true if string contains any of the terms:
terms.map((term) => string.includes(term)).includes(true);
Three examples:
terms.map((term) => 'Got term2 here'.includes(term)).includes(true); //true
terms.map((term) => 'Not here'.includes(term)).includes(true); //false
terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true); //true
Or, if you want to wrap the code up into a reusable hasTerm() function:
function hasTerm(string, terms) {
function search(term) { return string.includes(term); }
return terms.map(search).includes(true);
}
hasTerm('Got term2 here', terms); //true
hasTerm('Not here', terms); //false
hasTerm('Got term1 and term3', terms); //true
Try it out:
https://codepen.io/anon/pen/MzKZZQ?editors=0012
.map() documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Notes:
- This answer optimizes for simplicity and readability. If extremely large arrays of terms are expected, use a loop that short-circuits once a term is found.
- To support IE, transpile to replace occurrences of
.includes(x)with.indexOf(x) !== -1and=>with afunctiondeclaration.
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
You could use a loop. Maybe even create a helper function like so:
function ContainsAny(str, items){
for(var i in items){
var item = items[i];
if (str.indexOf(item) > -1){
return true;
}
}
return false;
}
Which you can then call like so:
if(ContainsAny(str, ["term1", "term2", "term3"])){
//do something
}
From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
– Anthony Grist
Mar 4 '13 at 12:56
@AnthonyGrist: Yeah I seen that already and just changed it
– musefan
Mar 4 '13 at 12:57
add a comment |
up vote
7
down vote
You could use a loop. Maybe even create a helper function like so:
function ContainsAny(str, items){
for(var i in items){
var item = items[i];
if (str.indexOf(item) > -1){
return true;
}
}
return false;
}
Which you can then call like so:
if(ContainsAny(str, ["term1", "term2", "term3"])){
//do something
}
From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
– Anthony Grist
Mar 4 '13 at 12:56
@AnthonyGrist: Yeah I seen that already and just changed it
– musefan
Mar 4 '13 at 12:57
add a comment |
up vote
7
down vote
up vote
7
down vote
You could use a loop. Maybe even create a helper function like so:
function ContainsAny(str, items){
for(var i in items){
var item = items[i];
if (str.indexOf(item) > -1){
return true;
}
}
return false;
}
Which you can then call like so:
if(ContainsAny(str, ["term1", "term2", "term3"])){
//do something
}
You could use a loop. Maybe even create a helper function like so:
function ContainsAny(str, items){
for(var i in items){
var item = items[i];
if (str.indexOf(item) > -1){
return true;
}
}
return false;
}
Which you can then call like so:
if(ContainsAny(str, ["term1", "term2", "term3"])){
//do something
}
edited Jan 3 '14 at 15:46
answered Mar 4 '13 at 12:49
musefan
39.4k1696148
39.4k1696148
From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
– Anthony Grist
Mar 4 '13 at 12:56
@AnthonyGrist: Yeah I seen that already and just changed it
– musefan
Mar 4 '13 at 12:57
add a comment |
From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
– Anthony Grist
Mar 4 '13 at 12:56
@AnthonyGrist: Yeah I seen that already and just changed it
– musefan
Mar 4 '13 at 12:57
From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
– Anthony Grist
Mar 4 '13 at 12:56
From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
– Anthony Grist
Mar 4 '13 at 12:56
@AnthonyGrist: Yeah I seen that already and just changed it
– musefan
Mar 4 '13 at 12:57
@AnthonyGrist: Yeah I seen that already and just changed it
– musefan
Mar 4 '13 at 12:57
add a comment |
up vote
7
down vote
if (/term1|term2|term3/.test("your string")) {
//youre code
}
1
Explanation of downvote would be nice.
– elrado
Mar 4 '13 at 12:56
You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
– Ash Burlaczenko
Mar 4 '13 at 13:01
4
Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
– elrado
Mar 4 '13 at 13:06
explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
– guradio
Nov 8 at 8:58
add a comment |
up vote
7
down vote
if (/term1|term2|term3/.test("your string")) {
//youre code
}
1
Explanation of downvote would be nice.
– elrado
Mar 4 '13 at 12:56
You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
– Ash Burlaczenko
Mar 4 '13 at 13:01
4
Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
– elrado
Mar 4 '13 at 13:06
explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
– guradio
Nov 8 at 8:58
add a comment |
up vote
7
down vote
up vote
7
down vote
if (/term1|term2|term3/.test("your string")) {
//youre code
}
if (/term1|term2|term3/.test("your string")) {
//youre code
}
edited Jul 13 '15 at 13:22
answered Mar 4 '13 at 12:49
elrado
2,4511011
2,4511011
1
Explanation of downvote would be nice.
– elrado
Mar 4 '13 at 12:56
You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
– Ash Burlaczenko
Mar 4 '13 at 13:01
4
Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
– elrado
Mar 4 '13 at 13:06
explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
– guradio
Nov 8 at 8:58
add a comment |
1
Explanation of downvote would be nice.
– elrado
Mar 4 '13 at 12:56
You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
– Ash Burlaczenko
Mar 4 '13 at 13:01
4
Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
– elrado
Mar 4 '13 at 13:06
explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
– guradio
Nov 8 at 8:58
1
1
Explanation of downvote would be nice.
– elrado
Mar 4 '13 at 12:56
Explanation of downvote would be nice.
– elrado
Mar 4 '13 at 12:56
You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
– Ash Burlaczenko
Mar 4 '13 at 13:01
You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
– Ash Burlaczenko
Mar 4 '13 at 13:01
4
4
Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
– elrado
Mar 4 '13 at 13:06
Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
– elrado
Mar 4 '13 at 13:06
explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
– guradio
Nov 8 at 8:58
explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
– guradio
Nov 8 at 8:58
add a comment |
up vote
3
down vote
Maybe this:
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0)
{
//your code
}
+1 for the clear simple solution !
– Hussein Nazzal
Mar 4 '13 at 12:51
1
Not ideal for an unknown number of terms though
– musefan
Mar 4 '13 at 12:55
1
I agree, but he asked for 3 substrings.
– freshbm
Mar 4 '13 at 13:00
2
but he askedshort of using several instances of this code
– Arun P Johny
Mar 4 '13 at 13:16
add a comment |
up vote
3
down vote
Maybe this:
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0)
{
//your code
}
+1 for the clear simple solution !
– Hussein Nazzal
Mar 4 '13 at 12:51
1
Not ideal for an unknown number of terms though
– musefan
Mar 4 '13 at 12:55
1
I agree, but he asked for 3 substrings.
– freshbm
Mar 4 '13 at 13:00
2
but he askedshort of using several instances of this code
– Arun P Johny
Mar 4 '13 at 13:16
add a comment |
up vote
3
down vote
up vote
3
down vote
Maybe this:
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0)
{
//your code
}
Maybe this:
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0)
{
//your code
}
answered Mar 4 '13 at 12:48
freshbm
4,72023262
4,72023262
+1 for the clear simple solution !
– Hussein Nazzal
Mar 4 '13 at 12:51
1
Not ideal for an unknown number of terms though
– musefan
Mar 4 '13 at 12:55
1
I agree, but he asked for 3 substrings.
– freshbm
Mar 4 '13 at 13:00
2
but he askedshort of using several instances of this code
– Arun P Johny
Mar 4 '13 at 13:16
add a comment |
+1 for the clear simple solution !
– Hussein Nazzal
Mar 4 '13 at 12:51
1
Not ideal for an unknown number of terms though
– musefan
Mar 4 '13 at 12:55
1
I agree, but he asked for 3 substrings.
– freshbm
Mar 4 '13 at 13:00
2
but he askedshort of using several instances of this code
– Arun P Johny
Mar 4 '13 at 13:16
+1 for the clear simple solution !
– Hussein Nazzal
Mar 4 '13 at 12:51
+1 for the clear simple solution !
– Hussein Nazzal
Mar 4 '13 at 12:51
1
1
Not ideal for an unknown number of terms though
– musefan
Mar 4 '13 at 12:55
Not ideal for an unknown number of terms though
– musefan
Mar 4 '13 at 12:55
1
1
I agree, but he asked for 3 substrings.
– freshbm
Mar 4 '13 at 13:00
I agree, but he asked for 3 substrings.
– freshbm
Mar 4 '13 at 13:00
2
2
but he asked
short of using several instances of this code– Arun P Johny
Mar 4 '13 at 13:16
but he asked
short of using several instances of this code– Arun P Johny
Mar 4 '13 at 13:16
add a comment |
up vote
2
down vote
You can do something like
function isSubStringPresent(str){
for(var i = 1; i < arguments.length; i++){
if(str.indexOf(arguments[i]) > -1){
return true;
}
}
return false;
}
isSubStringPresent('mystring', 'term1', 'term2', ...)
Never seen this before, using extra arguments. Can you point me to the docs?
– Ash Burlaczenko
Mar 4 '13 at 12:53
Interesting, I never knew aboutargumentsbefore... though I assume this will be vulnerable to redefinition?
– musefan
Mar 4 '13 at 12:54
every javascript method get a hidden parameter calledargumentswhich is an array like structure with all the parameters passed to the method. It is like variable arguments in java
– Arun P Johny
Mar 4 '13 at 12:55
no, it is as per standard ECMA-262
– Arun P Johny
Mar 4 '13 at 12:58
2
On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
– Ash Burlaczenko
Mar 4 '13 at 13:06
|
show 2 more comments
up vote
2
down vote
You can do something like
function isSubStringPresent(str){
for(var i = 1; i < arguments.length; i++){
if(str.indexOf(arguments[i]) > -1){
return true;
}
}
return false;
}
isSubStringPresent('mystring', 'term1', 'term2', ...)
Never seen this before, using extra arguments. Can you point me to the docs?
– Ash Burlaczenko
Mar 4 '13 at 12:53
Interesting, I never knew aboutargumentsbefore... though I assume this will be vulnerable to redefinition?
– musefan
Mar 4 '13 at 12:54
every javascript method get a hidden parameter calledargumentswhich is an array like structure with all the parameters passed to the method. It is like variable arguments in java
– Arun P Johny
Mar 4 '13 at 12:55
no, it is as per standard ECMA-262
– Arun P Johny
Mar 4 '13 at 12:58
2
On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
– Ash Burlaczenko
Mar 4 '13 at 13:06
|
show 2 more comments
up vote
2
down vote
up vote
2
down vote
You can do something like
function isSubStringPresent(str){
for(var i = 1; i < arguments.length; i++){
if(str.indexOf(arguments[i]) > -1){
return true;
}
}
return false;
}
isSubStringPresent('mystring', 'term1', 'term2', ...)
You can do something like
function isSubStringPresent(str){
for(var i = 1; i < arguments.length; i++){
if(str.indexOf(arguments[i]) > -1){
return true;
}
}
return false;
}
isSubStringPresent('mystring', 'term1', 'term2', ...)
edited Nov 8 at 8:56
guradio
13.7k31836
13.7k31836
answered Mar 4 '13 at 12:50
Arun P Johny
319k49421441
319k49421441
Never seen this before, using extra arguments. Can you point me to the docs?
– Ash Burlaczenko
Mar 4 '13 at 12:53
Interesting, I never knew aboutargumentsbefore... though I assume this will be vulnerable to redefinition?
– musefan
Mar 4 '13 at 12:54
every javascript method get a hidden parameter calledargumentswhich is an array like structure with all the parameters passed to the method. It is like variable arguments in java
– Arun P Johny
Mar 4 '13 at 12:55
no, it is as per standard ECMA-262
– Arun P Johny
Mar 4 '13 at 12:58
2
On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
– Ash Burlaczenko
Mar 4 '13 at 13:06
|
show 2 more comments
Never seen this before, using extra arguments. Can you point me to the docs?
– Ash Burlaczenko
Mar 4 '13 at 12:53
Interesting, I never knew aboutargumentsbefore... though I assume this will be vulnerable to redefinition?
– musefan
Mar 4 '13 at 12:54
every javascript method get a hidden parameter calledargumentswhich is an array like structure with all the parameters passed to the method. It is like variable arguments in java
– Arun P Johny
Mar 4 '13 at 12:55
no, it is as per standard ECMA-262
– Arun P Johny
Mar 4 '13 at 12:58
2
On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
– Ash Burlaczenko
Mar 4 '13 at 13:06
Never seen this before, using extra arguments. Can you point me to the docs?
– Ash Burlaczenko
Mar 4 '13 at 12:53
Never seen this before, using extra arguments. Can you point me to the docs?
– Ash Burlaczenko
Mar 4 '13 at 12:53
Interesting, I never knew about
arguments before... though I assume this will be vulnerable to redefinition?– musefan
Mar 4 '13 at 12:54
Interesting, I never knew about
arguments before... though I assume this will be vulnerable to redefinition?– musefan
Mar 4 '13 at 12:54
every javascript method get a hidden parameter called
arguments which is an array like structure with all the parameters passed to the method. It is like variable arguments in java– Arun P Johny
Mar 4 '13 at 12:55
every javascript method get a hidden parameter called
arguments which is an array like structure with all the parameters passed to the method. It is like variable arguments in java– Arun P Johny
Mar 4 '13 at 12:55
no, it is as per standard ECMA-262
– Arun P Johny
Mar 4 '13 at 12:58
no, it is as per standard ECMA-262
– Arun P Johny
Mar 4 '13 at 12:58
2
2
On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
– Ash Burlaczenko
Mar 4 '13 at 13:06
On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
– Ash Burlaczenko
Mar 4 '13 at 13:06
|
show 2 more comments
up vote
0
down vote
The .map() function can be used to convert an array of terms into an array of booleans indicating if each term is found. Then check if any of the booleans are true.
Given an array of terms:
const terms = ['term1', 'term2', 'term3'];
This line of code will return true if string contains any of the terms:
terms.map((term) => string.includes(term)).includes(true);
Three examples:
terms.map((term) => 'Got term2 here'.includes(term)).includes(true); //true
terms.map((term) => 'Not here'.includes(term)).includes(true); //false
terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true); //true
Or, if you want to wrap the code up into a reusable hasTerm() function:
function hasTerm(string, terms) {
function search(term) { return string.includes(term); }
return terms.map(search).includes(true);
}
hasTerm('Got term2 here', terms); //true
hasTerm('Not here', terms); //false
hasTerm('Got term1 and term3', terms); //true
Try it out:
https://codepen.io/anon/pen/MzKZZQ?editors=0012
.map() documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Notes:
- This answer optimizes for simplicity and readability. If extremely large arrays of terms are expected, use a loop that short-circuits once a term is found.
- To support IE, transpile to replace occurrences of
.includes(x)with.indexOf(x) !== -1and=>with afunctiondeclaration.
add a comment |
up vote
0
down vote
The .map() function can be used to convert an array of terms into an array of booleans indicating if each term is found. Then check if any of the booleans are true.
Given an array of terms:
const terms = ['term1', 'term2', 'term3'];
This line of code will return true if string contains any of the terms:
terms.map((term) => string.includes(term)).includes(true);
Three examples:
terms.map((term) => 'Got term2 here'.includes(term)).includes(true); //true
terms.map((term) => 'Not here'.includes(term)).includes(true); //false
terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true); //true
Or, if you want to wrap the code up into a reusable hasTerm() function:
function hasTerm(string, terms) {
function search(term) { return string.includes(term); }
return terms.map(search).includes(true);
}
hasTerm('Got term2 here', terms); //true
hasTerm('Not here', terms); //false
hasTerm('Got term1 and term3', terms); //true
Try it out:
https://codepen.io/anon/pen/MzKZZQ?editors=0012
.map() documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Notes:
- This answer optimizes for simplicity and readability. If extremely large arrays of terms are expected, use a loop that short-circuits once a term is found.
- To support IE, transpile to replace occurrences of
.includes(x)with.indexOf(x) !== -1and=>with afunctiondeclaration.
add a comment |
up vote
0
down vote
up vote
0
down vote
The .map() function can be used to convert an array of terms into an array of booleans indicating if each term is found. Then check if any of the booleans are true.
Given an array of terms:
const terms = ['term1', 'term2', 'term3'];
This line of code will return true if string contains any of the terms:
terms.map((term) => string.includes(term)).includes(true);
Three examples:
terms.map((term) => 'Got term2 here'.includes(term)).includes(true); //true
terms.map((term) => 'Not here'.includes(term)).includes(true); //false
terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true); //true
Or, if you want to wrap the code up into a reusable hasTerm() function:
function hasTerm(string, terms) {
function search(term) { return string.includes(term); }
return terms.map(search).includes(true);
}
hasTerm('Got term2 here', terms); //true
hasTerm('Not here', terms); //false
hasTerm('Got term1 and term3', terms); //true
Try it out:
https://codepen.io/anon/pen/MzKZZQ?editors=0012
.map() documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Notes:
- This answer optimizes for simplicity and readability. If extremely large arrays of terms are expected, use a loop that short-circuits once a term is found.
- To support IE, transpile to replace occurrences of
.includes(x)with.indexOf(x) !== -1and=>with afunctiondeclaration.
The .map() function can be used to convert an array of terms into an array of booleans indicating if each term is found. Then check if any of the booleans are true.
Given an array of terms:
const terms = ['term1', 'term2', 'term3'];
This line of code will return true if string contains any of the terms:
terms.map((term) => string.includes(term)).includes(true);
Three examples:
terms.map((term) => 'Got term2 here'.includes(term)).includes(true); //true
terms.map((term) => 'Not here'.includes(term)).includes(true); //false
terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true); //true
Or, if you want to wrap the code up into a reusable hasTerm() function:
function hasTerm(string, terms) {
function search(term) { return string.includes(term); }
return terms.map(search).includes(true);
}
hasTerm('Got term2 here', terms); //true
hasTerm('Not here', terms); //false
hasTerm('Got term1 and term3', terms); //true
Try it out:
https://codepen.io/anon/pen/MzKZZQ?editors=0012
.map() documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Notes:
- This answer optimizes for simplicity and readability. If extremely large arrays of terms are expected, use a loop that short-circuits once a term is found.
- To support IE, transpile to replace occurrences of
.includes(x)with.indexOf(x) !== -1and=>with afunctiondeclaration.
edited Nov 8 at 8:50
answered Nov 8 at 8:26
Dem Pilafian
2,73612548
2,73612548
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15201939%2fjquery-javascript-check-string-for-multiple-substrings%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Put it in a loop?
– musefan
Mar 4 '13 at 12:46
1
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0)How difficult could it be?– Alvin Wong
Mar 4 '13 at 12:48