All in one function for transforming word casings
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs/assets Add logo Dec 21, 2018
.gitignore Add initial working version with test coverage Apr 4, 2018
.travis.yml Update Dec 7, 2018
LICENSE Add logo Dec 21, 2018


All in one function for transforming word casings

Check out 🥁 BattleCry

casex was created as part of 🥁 BattleCry to allow versatile casings with text files.

The casex stardard used on BattleCry is also available separately through battle-casex


Previous versions

Although for most cases it will work just fine, casex 3.x is not fully compatible previous versions. If you need previous docs please refer to:


📦 343B gziped

npm install --save casex



import casex from 'casex';

casex(text, pattern);
casex('john doe', 'Ca Se'); // John Doe

How it works

1. Breaking text into words

By default, casex uses capitalizations (A-Z), -, _ and spaces (\s) to break the text into words.

Let's take for example i_am the-real JohnDoe:

  • i: 1st word
  • am: 2nd+ word
  • the: 2nd+ word
  • real: 2nd+ word
  • John: 2nd+ word
  • Doe: 2nd+ word

1.1 Custom delimiters

The default will likely work for most of your cases, but if you wish, you can provide custom delimiters:

casex(',baz', 'Ca Se', '.,'); // Foo Bar Baz

_Note: The default delimiters are: A-Z\\s_-.

2. Applying capitalization pattern and gluing words together

Let's take for example Ca_se:

  • C: first letter of the first word
  • a: second and subsequent letters of the first word
  • _: anything between the first two and last two letters is glue and will be repeted between words
  • s: first letter of the second and subsequent words
  • e: second and subsequent letters of the second and subsequent words

Confusing? Check out the demo and/or examples below. I'm sure you'll get the hang of it :)

Note: You could use any other letters to describe, such as aa$aa or na_me. What matters is that it takes the first two and last two letters for checking capitalization and whatever is in the middle is "glue".

2.1 Special transformations

Besides using lower and uppercase letters, you can also use:

  • *: Do not change word
  • -: Remove word


For these examples I'll use the text i_am the-real JohnDoe


  • Pattern: case
  • Output: iamtherealjohndoe


  • Pattern: CASE


  • Pattern: ca_se
  • Output: i_am_the_real_john_doe


  • Pattern: ca-se
  • Output: i-am-the-real-john-doe


  • Pattern: caSe
  • Output: iAmTheRealJohnDoe


  • Pattern: CaSe
  • Output: IAmTheRealJohnDoe

Sentence case

  • Pattern: Ca se
  • Output: I am the real john doe

Title Case

  • Pattern: Ca Se
  • Output: I Am The Real John Doe

Weird Example

  • Pattern: Ca12 34Se
  • Output: I12 34Am12 34The12 34Real12 34John12 34Doe

Examples with special characters

Capitalize first letter

  • Pattern: C* **
  • Output: I am the real John Doe


  • Input: John Doe
  • Pattern: C-S-
  • Output: JD