Tutorial by Examples

Standard Creation It is recommended to use this form only when creating regex from dynamic variables. Use when the expression may change or the expression is user generated. var re = new RegExp(".*"); With flags: var re = new RegExp(".*", "gmi"); With a backsl...
There are several flags you can specify to alter the RegEx behaviour. Flags may be appended to the end of a regex literal, such as specifying gi in /test/gi, or they may be specified as the second argument to the RegExp constructor, as in new RegExp('test', 'gi'). g - Global. Finds all matches inst...
Match Using .exec() RegExp.prototype.exec(string) returns an array of captures, or null if there was no match. var re = /([0-9]+)[a-z]+/; var match = re.exec("foo123bar"); match.index is 3, the (zero-based) location of the match. match[0] is the full match string. match[1] is the t...
var re = /[a-z]+/; if (re.test("foo")) { console.log("Match exists."); } The test method performs a search to see if a regular expression matches a string. The regular expression [a-z]+ will search for one or more lowercase letters. Since the pattern matches the string,...
The String object has the following methods that accept regular expressions as arguments. "string".match(... "string".replace(... "string".split(... "string".search(... Match with RegExp console.log("string".match(/[i-n]+/)); console.log(&...
String#replace can have a function as its second argument so you can provide a replacement based on some logic. "Some string Some".replace(/Some/g, (match, startIndex, wholeString) => { if(startIndex == 0){ return 'Start'; } else { return 'End'; } }); // will retur...
JavaScript supports several types of group in it's Regular Expressions, capture groups, non-capture groups and look-aheads. Currently, there is no look-behind support. Capture Sometimes the desired match relies on it's context. This means a simple RegExp will over-find the piece of the String that...
Sometimes you doesn't want to simply replace or remove the string. Sometimes you want to extract and process matches. Here an example of how you manipulate matches. What is a match ? When a compatible substring is found for the entire regex in the string, the exec command produce a match. A match i...

Page 1 of 1