10x Developer
  • Introduction
  • HTML
    • HTML DOM Jquery
    • HTML: Form
    • CSS
    • CSS Layout
    • CSS Flexbox
    • CSS Grid
    • SASS/LESS
    • CSS/LESS/SASS Cookbook
    • Bootstrap
  • JavaScript
    • JavaScript
    • ES6
    • TypeScript
    • JavaScript Testing
    • JavaScript Event Loop
    • DOM
    • Web APIs
    • JSON
    • RegEx
    • Functional Programming
    • JavaScript Lib
    • CoffeeScript
    • CoffeeScript to ES6 cheatsheet
  • Angular.js
    • Angular
    • Angular Cookbook
    • Angular Mistakes I Made
    • Angular 1.x
  • React.js
    • React.js
  • Node
    • Node.js
    • CLI command line tool
    • Electron / Atom
    • NW.js (node-webkit)
  • Serverless
    • AWS Lambda
    • Google Cloud Function
    • Actions on Google / Google Assistant
  • Full Stack Development
    • HTTP
    • Meteor
    • MongoDB
    • Digital Ocean
    • UI
    • Sketch
    • Web Dev Resources
  • Lang
    • Ruby
  • Know Your Tools
    • Chrome DevTools
    • Editor: VS Code
    • Editor: Vim
    • Editor: Sublime
    • Editor: Atom
    • Windows
    • Git
    • Linux / Bash
    • Mac
  • Cheatsheets
Powered by GitBook
On this page
  • Links
  • Usage
  • Checklist
  • Examples
  • How to replace "name" : "basic" to "xxx" : "whatever you provided"?
  • Find the number after a certain word:
  • How to make sure string 'po box' is not in the test string:
  • How to find a pattern before a word:
  • Split using a maximum length (Ruby)
  • Match string insdie single quotes
  • match a word
  1. JavaScript

RegEx

PreviousJSONNextFunctional Programming

Last updated 6 years ago

Links

  • Test tool for js:

  • For Ruby:

  • Debug regex:

  • Cheat Sheet:

Usage

See

'str'.search(/pattern/) // returns index
/pattern/.test('str') // : boolean
/pattern/.exec('str') // return null or matches array ([0] is full string, [1].. is group

Checklist

  • Be careful about the greedy matching .*, try to add ? to use lazy matching: .*?. See

  • Positive lookahead (does incldue): (?=...)

  • Negative lookahead (not include multiple chars): (?!...)

    • to match a single char that's not in the list, you can use [^abc] (means match one char that's not a or b or c)

  • Positive lookbehind: (?<=...) NOTE JavaScript doens't have this. You can use negative lookahead

Examples

How to replace "name" : "basic" to "xxx" : "whatever you provided"?

// c#
string input = "'name' : 'Basic'," ;
string find = "('name'\\s:\\s')(?<text>.*)(')" ;
string replace = "$1AA$2" ;
string result = Regex.Replace (input, find, replace);
Console.WriteLine(result);

Find the number after a certain word:

(?<=%download%#)\d+

How to make sure string 'po box' is not in the test string:

Use negative lookahead: /^(?!.*po\sBOX).*$/

How to find a pattern before a word:

Let's say we want to match and replace :user in the url abc.com/:user/notebooks/:user-name/:userName Use We could use: /:user(?=\b)/, which will match the first :user and :user-name but not :userName.

Split using a maximum length (Ruby)

regex = /.{1,#{max_width}}/
text.scan(regex).join(zero_width_space)

Match string insdie single quotes

var result = [];
const pattern = /'([^',]+)'/g;
var match;
while(match = pattern.exec("'a','b', 'c'")) {
    result.push(match[1]);
}
console.log(result);

match a word

  • use word boundary: \b

  • be sure to be lazy (+?)

const re = /^\/(.+?)\b/;
re.exec('/abc/def')[1] // -> abc
re.exec('/abc-def')[1] // -> abc
re.exec('/abc_def')[1] // -> abc_def

using grouping for everything:

The lookbehind assertion (?<=foo_bar) is important because you don't want to include %download%# in your results, only the numbers after it.

https://regex101.com/#javascript
http://rubular.com/
https://www.debuggex.com/
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/pdf/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search
here
http://stackoverflow.com/a/6005637/166286
http://stackoverflow.com/questions/4740984/c-sharp-regex-matches-example
https://regex101.com/r/uH6uK3/1