JavaScript Regex Examples
JS
S
JavaScriptExamples of Regular Expressions usage in JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
1/*
2
3CHEATSHEET
4
5. — (period) Matches any single character, except for line breaks.
6* — Matches the preceding expression 0 or more times.
7+ — Matches the preceding expression 1 or more times.
8? — Preceding expression is optional (Matches 0 or 1 times).
9^ — Matches the beginning of the string.
10$ — Matches the end of the string.
11
12\d OR [0-9] — Matches any single digit character.
13\w OR [A-Za-z0-9_] — Matches any word character (alphanumeric & underscore).
14[XYZ] — Character Set: Matches any single character from the character within the brackets. You can also do a range such as [A-Z]
15[XYZ]+ — Matches one or more of any of the characters in the set.
16[^A-Z] — Inside a character set, the ^ is used for negation. In this example, match anything that is NOT an uppercase letter.
17
18g — Global search
19i — case insensitive search
20
21(x) — Capturing Parenthesis: Matches x and remembers it so we can use it later.
22(?:x) — Non-capturing Parenthesis: Matches x and does not remembers it.
23x(?=y) — Lookahead: Matches x only if it is followed by y.
24*/
25
26// Replace word starting at:
27/\b(\w*surveys_questions_key\w*)\b/
28
29// Extract all image urls
30 var urlRegex = new RegExp(/(https?:\/\/www.coderecipes.org).([a-z\.]{2,6})([\/\w\.-]*)*\/?\S/g);
31
32// Unix Path
33const regex = /((?:\/[\w\.\-]+)+)([a-z])/i;
34const extraction = regex.exec(unixPath);
35
36/* String Replace http* with an HTML anchor
37(http\S+) is a capturing group which captures 'http' followed by 1 or more non-whitespace characters (\S is a non-whitespace character)
38*/
39const anchorified = value.replace(/(http\S+)/gi, '<a href="$1"> $1 </a>');
40
41// Anything within parenthesis
42const regexAnythingWithinParenthesis = \(([^\)]+)\)
43
44// Has string
45const regexHasPort = /port/;
46const regexHasPortViaConstructor = new RegExp('port', 'i');
47const result = regexHasPort.test('port number is 9095'); //true
48
49// Has numbers only
50const regexOnlyNumbers = /\d/;
51const result = regexOnlyNumbers.test('foo bar'); //false
52const result1 = regexOnlyNumbers.test('12'); //true
53
54// Has 4 numbers with dashes in exact sequence
55const regexOnly4NumbersWithDashes = /\d-\d-\d-\d/;
56const result = regexOnly4NumbersWithDashes.test('1-2-3-4'); //true
57const result1 = regexOnly4NumbersWithDashes.test('1-x-3-4'); //false
58const result2 = regexOnly4NumbersWithDashes.test('1-3-4'); //false
59
60// Has n numbers separated with dashes
61const regexOnlyNumbersWithDashes = /\d+-\d+/;
62const result = regexOnlyNumbersWithDashes.test('1-2-3-4'); //true
63const result1 = regexOnlyNumbersWithDashes.test('12222222-222222'); //true
64const result2 = regexOnlyNumbersWithDashes.test('foo-bar'); //false
65
66// Name Identifier (match: 'pod' once, numbers 1+ times, 'id' once)
67const regexNameIdentifier = /pod(\d)+id/;
68const result = regexNameIdentifier.test('pod1223id'); //true
69const result = regexNameIdentifier.test('pod1223i'); //false
70const result = regexNameIdentifier.test('podXXid'); //false
71
72// Name Identifier (optional: 'pod' once, any number)
73const regexNameIdentifierOptional = /(pod)?(\d)+/i;
74const result = regexNameIdentifierOptional.test('pod122333'); //true
75const result = regexNameIdentifierOptional.test('rc122333'); //true
76const result = regexNameIdentifierOptional.test('rcXXX'); //false
77
78// Capital Letters
79const regexCapitalLetters = /[A-Z]/g;
80const result = regexCapitalLetters.test('AAs');
81
82// Capture Data and Use it Later
83const regexCapture = /([A-Z])/;
84
85
86// removeCamelCase Function
87function removeCamelCase(str){
88 //Replace each capital Letter with a space and that Letter
89 const addSpaces = str.replace(/([A-Z])/g, ' $1');
90 const transformToLowerCase = addSpaces.replace(/([A-Z])/g, c => c.toLowerCase());
91 const upperFirstLetter = transformToLowerCase.replace(/^[a-z]/, c => c.toUpperCase());
92 const addDot = upperFirstLetter.replace(/(\w)$/, '$1.');
93 return addDot;
94}
95const result = removeCamelCase('myFunction');
96
97// Split string keeping the delimiter
98const splitByHttp = value.split(/(?=http)/);
99
100// Test World Country Codes
101const regex = /^(AF|AX|AL|DZ|AS|AD|AO|AI|AQ|AG|AR|AM|AW|AU|AT|AZ|BS|BH|BD|BB|BY|BE|BZ|BJ|B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)?[0-9]S[0-9]{5}L|(IT)?[0-9]{11}|(LT)?([0-9]{9}|[0-9]{12})|(LU)?[0-9]{8}|(LV)?[0-9]{11}|(MT)?[0-9]{8}|(NL)?[0-9]{9}B[0-9]{2}|(PL)?[0-9]{10}|(PT)?[0-9]{9}|(RO)?[0-9]{2,10}|(SE)?[0-9]{12}|(SI)?[0-9]{8}|(SK)?[0-9]{10})$/;
107const frenchVAT = regexEuropeanVATCodes.test('FRXX999999999'));
108
109// Test European Countries
110const regexEuropeanCountries = /AT|BE|BG|CY|CZ|DE|DK|EE|EL|GR|ES|FI|FR|GB|HU|IE|IT|LT|LU|LV|MT|NL|PL|PT|RO|SE|SI|SK/;
111
112// Everything from character to the end of the line (eg. good for find&replace)
113'.*
114
115// Find all numbers after the slash
116{ "appointments.bookingReference" : {$regex: "\/(525727)(?=[^\/]*$)", $options:"$i" } }
117Created on 11/24/2017