search() and includes(), because what's life without a little RegExp
search() and includes() for RegExp-related fun and joy, respectively. One of them doesn't accept RegExp as a parameter.
Table of contents
A short RegExp intro
A regular expression (or RegExp, or regex, and everything in between) is a group of characters used to find a pattern in text, starting from the left. It has many uses ranging from searches and validations, to substring extraction and text replacement.
It's a topic for entire books, so not something I'd be able to easily cover here, but if you're interested in learning more, I can recommend: RegExr.com Regex Generator Simple RegEx tutorial Learn RegEx
Search
search()
will return a RegExp's starting index inside a String. If no match is found, then it'll return -1
.
Only the first match will be returned, so it's no use trying to use the global g
flag of RegExp in search()
.
Let's try and use search using the following regular expressions:
let string = 'Well, whatdya know, there actually IS a candy mountain.'
let regex1 = /,/
string.search(regex1)
//output: 4
let regex2 = /\./
string.search(regex2)
//output: 54
// to find something that is NOT a lowercase vowel, not a 'w' in any case, and not a space
let regex3 = /[^Wwaeiou\s]/
string.search(regex3)
//output: 2
// regular expression to find a white space followed by a capital letter:
let regex4 = /\s+[A-Z]/
string.search(regex4)
//output: 34
let regex5 = /A/
string.search(regex5)
//output: -1
Notice that regex5
returned -1 because there is no uppercase "A" in the string we provided.
Ok so we got the indexes back. What if we want to see what exactly string.search(regex3)
found?
let string = 'Well, whatdya know, there actually IS a candy mountain.'
let regex3 = /[^Wwaeiou\s]/
string[string.search(regex3)]
//output: "l"
let indexFromRegex3 = string.search(regex3)
string[indexFromRegex3]
//output: "l"
let regex5 = /A/
string[string.search(regex5)]
//output: undefined
Includes
In the case of includes()
, RegExp is not allowed as a parameter. This method will return true
or false
depending on whether a string can be found within another.
It takes two parameters, the second defaulting to 0
if not provided.
The first parameter is the string you want to check is included, and the second parameter is the position you want to start searching from.
Note that this method is case-sensitive, so if that's not important to you then you might want to turn the original string into lower or uppercase.
See it in action:
let string = 'Well, whatdya know, there actually IS a candy mountain.'
string.includes(' ')
//output: true
string.includes('what')
//output: true
string.includes('IS')
//output: true
string.includes('is')
//output: false
string.toLowerCase().includes('is')
//output: true
string.toLowerCase().includes('well', 0)
//output: true
string.toLowerCase().includes('well', 1)
//output: false
From the last example, starting from index 1 the closest we can get to the string "well" is "ell", so it's a false
right in our face.
That's it for search()
and includes()
!
A little practice!
If you'd like a little practice, try these katas:
JsKatas also has exercises on includes()