This is a forward-looking answer, and won't work in current implementations.
ECMAScript 6 is currently defining a String.prototype.contains
method. This will allow you to do:
if (url.contains(substring)) {
Again, this is a future addition. Currently ECMAScript 6 (Harmony) is being drafted, and this could technically be removed, though it doesn't seem likely.
Current draft:
15.5.4.24 String.prototype.contains (searchString, position = 0 )
The contains method takes two arguments, searchString and position, and performs the following steps:
- Let
O
be CheckObjectCoercible(this value)
.
- Let
S
be ToString(O)
.
ReturnIfAbrupt(S)
.
- Let
searchStr
be ToString(searchString)
.
ReturnIfAbrupt(searchStr)
.
- Let
pos
be ToInteger(position)
. (If position
is undefined
, this step produces the value 0
).
ReturnIfAbrupt(pos)
.
- Let
len
be the number of elements in S
.
- Let
start
be min(max(pos, 0), len)
.
- Let
searchLen
be the number of characters in searchStr
.
- If there exists any integer
k
not smaller than start such that k + searchLen
is not greater than len
, and for all nonnegative integers j
less than searchLen
, the character at position k+j
of S
is the same as the character at position j
of searchStr
, return true
; but if there is no such integer k
, return false
.