Skip to content

Instantly share code, notes, and snippets.

@lukehedger
Last active December 31, 2015 15:29
Show Gist options
  • Save lukehedger/8006855 to your computer and use it in GitHub Desktop.
Save lukehedger/8006855 to your computer and use it in GitHub Desktop.
RegExp collection
// basics
l = /^text$/, // literal notation
c = new RegExp("^text$"), // constructor notation
ltest = l.test(str), // test for expression, evaluates to true or false
ctest = c.test(str); // same test for both notations
// matching strings
var str = "this is a test";
start = /^this/ // => true
end = /test$/ // => true
whole = /^this$/ // => false
// extracting strings
r = /is a(.*)/ // any text after text in string
r = /^this is (.*)/ // any text after start of string
var match = str.match(r)[1] // return result
// extracting strings between points
url = 'http://www.someurl.com/id=XYZ?q=test'
id = url.match(/id=(.*)+?\?/i)[1] // => XYZ (text between id= and ?) - ? is escaped like \? so replace \? with any other text
url = 'abc?xyz=123&gl=GB&hl=en-GB&lang=en-GB'
gl = url.match(/gl=(.*?)\&/i) || src.match(/gl=(.*)/i) // => matches any text (.*) between gl= and &. A ? is added (.*?) to make the match non-greedy, otherwise it would continue to the last & it can find
lang = url.match(/lang=(.*?)\&/i) || src.match(/lang=(.*)/i) // => the second expression will be used here as lang is at the end of the string, not succeeded by an &. Both expressions are provided in case the order of parameters changes
// split string into chunks
r = /.{1,n}/g // where . = all characters, {1,n} matches at least 1 and at most 3 occurrences of the preceding item
n = 3
str.match(r) // => ["thi"," is"," a ","tes","t"]
// remove non-alphanumeric characters from string
r = /\W/g // \W = matches any non-alphanumeric chars
str.replace(r, "")
// remove non-alphanumeric characters except spaces and commas
r = /[^\w, ]+/ // [^...] = matches anything NOT enclosed in the square brackets, /w, = matches any alphanumeric chars, commas and spaces, + = enables multiple matches of preceding item rather than a single match
str.replace(r, "")
// add comma separators to numbers
r = /\B(?=(\d{3})+(?!\d))/g
str.replace(r, ",")
@lukehedger
Copy link
Author

Resources:

MDN resource

A couple of playgrounds for dissecting your expressions:
regexr
regex101
regexplained.co.uk
leaverou.github.io/regexplained

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment