About many things

I know, I'll use regular expressions!

Famous quote says:

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Let’s see, if it is true and when you can use regular expressions.

I will share my knowledge of regexp in JavaScript based on this source.

Basics

Regular expressions are used with the RegExp methods test and exec and with the String methods match, replace, search, and split.

  • exec - A RegExp method that executes a search for a match in a string. It returns an array of information or null on a mismatch.

  • test - A RegExp method that tests for a match in a string. It returns true or false.

  • match - A String method that executes a search for a match in a string. It returns an array of information or null on a mismatch.

  • search - A String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails.

  • replace - A String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.

  • split - A String method that uses a regular expression or a fixed string to break a string into an array of substrings.

// Sample with replace 
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
console.log(newstr);
// => "Smith, John".

Advanced searching with flags

Regular expressions have four optional flags that allow for global and case insensitive searching. These flags can be used separately or together in any order, and are included as part of the regular expression.

  • g - Global search.
  • i - Case-insensitive search.
  • m - Multi-line search.
  • y - Perform a “sticky” search that matches starting at the current position in the target string.

To include a flag with the regular expression, use this syntax:

var re = /pattern/flags;
// or by using RegExp
var re = new RegExp("pattern", "flags");

When you should not use regexp?

As mentioned in this answer:

Regular expressions are the wrong tool for the job because you are dealing with nested structures, i.e. recursion.
#regexp #javascript