KMX
KMX

Reputation: 2691

Putting space in camel case string using regular expression

I am driving my question from add a space between two words.

Requirement: Split a camel case string and put spaces just before the capital letter which is followed by a small case letter or may be nothing. The space should not incur between capital letters.

eg: CSVFilesAreCoolButTXT is a string I want to yield it this way CSV Files Are Cool But TXT

I drove a regular express this way:

"LightPurple".replace(/([a-z])([A-Z])/, '$1 $2')

If you have more than 2 words, then you'll need to use the g flag, to match them all.

"LightPurpleCar".replace(/([a-z])([A-Z])/g, '$1 $2')

If are trying to split words like CSVFile then you might need to use this regexp instead:

"CSVFilesAreCool".replace(/([a-zA-Z])([A-Z])([a-z])/g, '$1 $2$3')

But still it does not serve the way I have put my requirements.

Upvotes: 21

Views: 23038

Answers (6)

blazkovicz
blazkovicz

Reputation: 792

For those who want also lower capital letters of all words except first.

function PascalCaseToText(str) {
  return str.replace(/([a-z])([A-Z])/g, function(_, g1, g2) { return g1 + ' ' + g2.toLowerCase();  })
}

PascalCaseToText("SomePascalString");
// Some pascal string

Upvotes: 0

Sandeep Muthangi
Sandeep Muthangi

Reputation: 131

This worked for me

let camelCase = "CSVFilesAreCoolButTXTRules"
let re = /[A-Z-_\&](?=[a-z0-9]+)|[A-Z-_\&]+(?![a-z0-9])/g
let delimited = camelCase.replace(re,' $&').trim()

The above code works for almost all the use cases i had. I had a few peculiarities where '&' and '_' should be treated equivalent to an upper case character

  • ThisIsASlug ---> This Is A Slug
  • loremIpsum ---> lorem Ipsum
  • PAGS_US ---> PAGS_US
  • TheCapitalOfTheUAEIsAbuDhabi ---> The Capital Of The UAE Is Abu Dhabi
  • eclipseRCPExt ---> eclipse RCP Ext
  • VALUE ---> VALUE
  • SG&A ---> SG&A

A brief explanation

[A-Z-_\&](?=[a-z0-9]+)
//Matches normal words i.e. one uppercase followed by one or more non-uppercase characters 


[A-Z-_\&]+(?![a-z0-9]) 
//Matches acronyms & abbreviations i.e. a sequence of uppercase characters that are not followed by non-uppercase characters

Check out the regexr fiddle here

Upvotes: 2

Zach Wymer
Zach Wymer

Reputation: 540

Camel-case replacement for Javascript using lookaheads / behinds:

"TheCapitalOfTheUAEIsAbuDhabi".replace(/([A-Z](?=[a-z]+)|[A-Z]+(?![a-z]))/g, ' $1').trim()
// "The Capital Of The UAE Is Abu Dhabi"

Upvotes: 0

Husam Ebish
Husam Ebish

Reputation: 6828

Splitting CamelCase with regex in .NET :

Regex.Replace(input, "((?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z]))", " $1").Trim();

Example :

Regex.Replace("TheCapitalOfTheUAEIsAbuDhabi", "((?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z]))", " $1").Trim();

Output : The Capital Of The UAE Is Abu Dhabi

Upvotes: 3

Muhammad Adeel
Muhammad Adeel

Reputation: 2884

If the first character is always lowercase.

'camelCaseString'.replace(/([A-Z]+)/g, ' $1')

If the first character is uppercase.

'CamelCaseString'.replace(/([A-Z]+)/g, ' $1').replace(/^ /, '')

Upvotes: 6

MikeM
MikeM

Reputation: 13641

var rex = /([A-Z])([A-Z])([a-z])|([a-z])([A-Z])/g;

"CSVFilesAreCoolButTXT".replace( rex, '$1$4 $2$3$5' );
// "CSV Files Are Cool But TXT"

And also

"CSVFilesAreCoolButTXTRules".replace( rex, '$1$4 $2$3$5' );    
// "CSV Files Are Cool But TXT Rules"

The text of the subject string that matches the regex pattern will be replaced by the replacement string '$1$4 $2$3$5', where the $1, $2 etc. refer to the substrings matched by the pattern's capture groups ().

$1 refers to the substring matched by the first ([A-Z]) sub-pattern, and $3 refers to the substring matched by the first ([a-z]) sub-pattern etc.

Because of the alternation character |, to make a match the regex will have to match either the ([A-Z])([A-Z])([a-z]) sub-pattern or the ([a-z])([A-Z]) sub-pattern, so if a match is made several of the capture groups will remain unmatched. These capture groups can be referenced in the replacement string but they have have no effect upon it - effectively, they will reference an empty string.

The space in the replacement string ensures a space is inserted in the subject string every time a match is made (the trailing g flag means the regular expression engine will look for more than one match).

Upvotes: 34

Related Questions