Passing a String When a regexp is Expected

One last thing to note is that the four methods you just saw (split(), match(), search(), and replace()) can also take strings as opposed to regular expressions. In this case the string argument is used to produce a new regex as if it was passed to new RegExp().

Example of passing a string to replace:

"test".replace('t', 'r')
"rest"

The above is the same as:

"test".replace(new RegExp('t'), 'r')
"rest"

When you pass a string, you cannot set modifiers as you can with a normal constructor or regex literal.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +