Regexcavate

Dig into regular expressions

Regexcavate // Dig in to regular expressions

Uh-oh, it seems like you’ve tried to put a regex in the field. That’s not how this works.

How To

To begin: break up what you want to match into parts in plain English and enter them in the field above with each part separated by a comma. When you’re done, press enter/return.

 
  copy

Hover over this regex to see how it relates to what you put in the field. Want a more visual representation of this regex? Try it out over at regexplained.co.uk.

Not quite sure how to use this regex? Find how it works with the language you’re using at regular-expressions.info/tools.html.

I’m Confused

Regexcavate is a natural language “translator” for regex. Enter plain English words that describe what you'd like to match and get back a regular expression.

For more ideas, refresh the page for a random placeholder.

You can also use one of these pre-defined shortcuts:

    About

    If you’re involved in writing code of any sort, you’ve no doubt heard of regular expressions. But most people would agree that when you first look at them, it can feel a little like deciphering hieroglyphics.

    That’s why we created Regexcavate - at it's heart, a natural language “translator” for regular expressions. The aim is to be a learning tool for digging into regex and finding out how they can be put together. Even our logo is a regular expression. How meta.

    This tool is still in it’s infancy and dealing with a pretty vast topic; if you’re a regex guru then you might find it can’t do some of the more complex things you know regex is capable of. If you’d like to help us make this tool better, or if something doesn’t work that you think should please read about how to contribue below.

    To find out more about this tool and dig deeper, check out the repo over at github.

    Tutorial

    If you’re new to regex, and want a bit of a quick intro these slides should help get you started.

    So you’ve got your regex, what now? Here’s a quick tutorial of what you can do with it. We’ll look at two useful ways of using regex in Javascript: test and replace

    Test

    Let’s say you have a contact form that has a field for a reference number for your account (say for a bank or utility service). The company’s reference numbers are expected in the form of 1000000AA where A is any letter and 0 is any number. That could be described as: "1, 6 numbers, 2 letters".

    Try to regexcavate for that and you’ll get this:

    /1[0-9]{6}[a-zA-Z]{2}/

    Note: in Javascript you can define a regex literal by putting the expression between / characters. Regexcavate only gives you the expression, but we’re showing you the Javascript notation for demonstration purposes.

    So, how can this be used to check against user input? Well, we can use this pattern in a little Javascript function like so:

    var ref = document.getElementById('referenceNumber'); // the ID of reference number field
    var regex = /1[0-9]{6}[a-zA-Z]{2}/; // the pattern we’re expecting
    if ( regex.test(ref.value) ) {
      // do stuff if match found
    } else {
      // show error messages about reference format
    }

    For more information on the Javascript test() method, check out MDN for Regexp.test

    Replace

    Matching a pattern is useful - especially for more complex things like email addresses, usernames, phone numbers etc. but frequently you want to find and replace on a search pattern.

    If you are using a text editor like Textmate, Sublime Text or Vim, you’ll probably find that it’s search and replace functionality will accept a regex as the search pattern. This means you can find stuff that you would not normally be able to.

    Let’s say you have some code that is littered with trailing whitespace. This can cause issues in some languages but is just generally a bit messy to look at. Well, you could match all whitespace at the end of a line and replace it with nothing to tidy things up.

    Try to regexcavate for "any whitespaces, at the end of the line" and you’ll get this:

    /\s+$/

    Now use this in a find and replace, searching for \s+$ and "replace all" with an empty field and you’ll be trailing whitespace free!

    You can also use the String.replace() method in Javascript. Here’s a quick example... we're rather fond of regex, so let's update the string below:

    var string = 'I hate regular expressions... hate them!';
    string = string.replace(/hate/g, 'love'); // "g" replaces all occurrences
    console.log(string); // 'I love regular expressions... love them!'

    Contribute

    To make this little tool as awesome and as useful as possible, we need all the help we can get - so that means yours! We’ve got lots of new features in the pipeline but would really appreciate input from the community.

    Add translations

    If you’re a Reg-expert then hop on over to the wiki on Github to check out all the stuff that makes this work behind the scenes. If you’d like to add a 'translation' all you need to do is fork the project, hammer out some regex and submit a pull request.

    Bugs

    Bugs are everywhere; we’ve tried to squash as many as possible but if you find something broken, either tell us @regexcavate or submit a fix (or both).

    Improve the words

    We’re developers and not the greatest with words. If you can explain this all a bit better or you’ve found a typo or non-existing word, you can fork us and submit a pull request for things like that too. Documentation is a huge deal so please feel free to contribute to that too.

    Feature Requests

    If you have any other suggestions or requests, please let us know.