Anyway that I can check for string inside a string in javascript?
Example: If I have 2 variable
var value1 = '|1|2|3|4|5|6'
var value2 = '1|2|3'
How can I check whether value2 is found in value1?
var contained = (value1.indexOf(value2) != -1);
contained will be true if and only if value2 is a substring of value1.
'|1|2|3|4|5|6'.match(/1\|2\|3/);
Note that you need to escape the |, since they have a special meaning in Regular Expressions. If this is too much of a hassle, Matthews indexOf method is easier.
I like to use regular expressions and javascript's .match() method:
if(value1.match(/1\|2\|3/)) {
// value2 is found in value1
}
.match() returns an array of the matches, or null if no matches are found.
Also note that the pipes, |, have to be escaped with a backslash, \, because they have special meaning within regular expressions. Specifically, they mean OR. So /1|2/ becomes 1 OR 2. While if we escape the | with a backslash like so:
/1\|2/
It becomes:
1|2