正则表达式到分裂骆驼的情况

我在 JavaScript 中有一个正则表达式,它使用以下代码(我随后从 给你获得)将我的驼峰大小写字符串分割为大写字母:

"MyCamelCaseString"
.replace(/([A-Z])/g, ' $1')
.replace(/^./, function(str){ return str.toUpperCase(); })

因此,结果是:

"My Camel Case String"

这是好事。但是,我想要更进一步。有没有人能帮我一个正则表达式,只有当前一个字符是小写,后一个字符是大写时,它才会分裂。

因此,上面的例子将是我所期望的结果,但如果我这样做:

"ExampleID"

然后我回来了:

"Example ID"

而不是

"Example I D"

因为它在每个大写字母处分裂,忽略之前的任何内容。

希望这是有意义的! 谢谢:)。

67081 次浏览

My guess is replacing /([A-Z])/ with /([a-z])([A-Z])/ and ' $1' with '$1 $2'

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

/([a-z0-9])([A-Z])/ for numbers counting as lowercase characters

console.log("MyCamelCaseStringID".replace(/([a-z0-9])([A-Z])/g, '$1 $2'))

"MyCamelCaseString".replace(/([a-z](?=[A-Z]))/g, '$1 ')

outputs:

"My Camel Case String"

This RegExp String is

.replace("/([a-zA-Z][a-z]*)/g",...);

If you want an array of lower case words:

"myCamelCaseString".split(/(?=[A-Z])/).map(s => s.toLowerCase());

If you want a string of lower case words:

"myCamelCaseString".split(/(?=[A-Z])/).map(s => s.toLowerCase()).join(' ');

If you want to separate the words but keep the casing:

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

Hi I saw no live demo , thanks @michiel-dral

var tests =[ "camelCase",
"simple",
"number1Case2",
"CamelCaseXYZ",
"CamelCaseXYZa"
]


function getCamelCaseArray(camel) {
var reg = /([a-z0-9])([A-Z])/g;
return camel.replace(reg, '$1 $2').split(' ');
}


function printTest(test) {
document.write('<p>'+test + '=' + getCamelCaseArray(test)+'</p>');
}


tests.forEach(printTest);
<!DOCTYPE html>
<html>


<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>


<body>
</body>


</html>

I prefer to work with arrays over strings. It's easier to debug and more flexible. This is an actual join instead of replace. I haven't dealt with white spaces in the strings but you could just trim each element easily enough.

const splitCamelCase = str => str.match(/^[A-Z]?[^A-Z]*|[A-Z][^A-Z]*/g).join(' ');


console.log(splitCamelCase('fooMyCamelCaseString'));
console.log(splitCamelCase('MyCamelCaseString'));
console.log(splitCamelCase('XYZMyCamelCaseString'));
console.log(splitCamelCase('alllowercase'));

Sometime camelCase strings include abbreviations, for example:

PDFSplitAndMergeSamples
PDFExtractorSDKSamples
PDFRendererSDKSamples
BarcodeReaderSDKSamples

And in this case the following function will work, it splits the string leaving abbreviations as separate strings:

function SplitCamelCaseWithAbbreviations(s){
return s.split(/([A-Z][a-z]+)/).filter(function(e){return e});
}

Example:

function SplitCamelCaseWithAbbreviations(s){
return s.split(/([A-Z][a-z]+)/).filter(function(e){return e});
}


console.log(SplitCamelCaseWithAbbreviations('PDFSplitAndMergeSamples'));
console.log(SplitCamelCaseWithAbbreviations('PDFExtractorSDKSamples'));
console.log(SplitCamelCaseWithAbbreviations('PDFRendererSDKSamples'));
console.log(SplitCamelCaseWithAbbreviations('BarcodeReaderSDKSamples'));

You can use a combination of regEx, replace, and trim.

"ABCMyCamelCaseSTR".replace(/([A-Z][a-z0-9]+)/g, ' $1 ')
.replace(/\s{2}/g," ").trim()


// ABC My Camel Case STR

Regex not-a word boundary \B character can also be used

console.log("MyCamelCaseString".replace(/(\B[A-Z])/g, ' $1'));

I found that none of the answers for this question really worked in all cases and also not at all for unicode strings, so here's one that does everything, including dash and underscore notation splitting.

let samples = [
"ThereIsWay_too  MuchCGIInFilms These-days",
"UnicodeCanBeCAPITALISEDTooYouKnow",
"CAPITALLetters at the StartOfAString_work_too",
"As_they_DoAtTheEND",
"BitteWerfenSie-dieFußballeInDenMüll",
"IchHabeUberGesagtNichtÜber",
"2BeOrNot2Be",
"ICannotBelieveThe100GotRenewed. It-isSOOOOOOBad"
];


samples.forEach(sample => console.log(sample.replace(/([^[\p{L}\d]+|(?<=[\p{Ll}\d])(?=\p{Lu})|(?<=\p{Lu})(?=\p{Lu}[\p{Ll}\d])|(?<=[\p{L}\d])(?=\p{Lu}[\p{Ll}\d]))/gu, '-').toUpperCase()));

If you don't want numbers treated as lower case letters, then:

let samples = [
"2beOrNot2Be",
"ICannotBelieveThe100GotRenewed. It-isSOOOOOOBad"
];


samples.forEach(sample => console.log(sample.replace(/([^\p{L}\d]+|(?<=\p{L})(?=\d)|(?<=\d)(?=\p{L})|(?<=[\p{Ll}\d])(?=\p{Lu})|(?<=\p{Lu})(?=\p{Lu}\p{Ll})|(?<=[\p{L}\d])(?=\p{Lu}\p{Ll}))/gu, '-').toUpperCase()));

a = 'threeBlindMice'
a.match(/[A-Z]?[a-z]+/g) // [ 'three', 'Blind', 'Mice' ]

is the simplest way I've found, for simple camel/titlecase splitting.

If you want to capitalize and add space between numbers as well, this works.

transform(value: string, ...args: any[]): string {
const str = 'this1IsASampleText';
str.charAt(0).toUpperCase() + value.slice(1); // Capitalize the first letter
str.replace(/([0-9A-Z])/g, ' $&'); // Add space between camel casing
}

Results:

This 1 Is A Sample Text

I recently came across this question and needed to do the exact same thing:

employeeID should be rendered as Employee ID

I found this convert case library from zellwk plus a little additional reduce function did the trick for me:

import { toTitle } from "./convert-case.js";


// NB. Assumes sequential single chars can be concatenated
// ex. N B A Finals => NBA Finals
const reducer = (total, currentValue, currentIndex, arr) => {
if (
currentValue.length === 1 &&
!(currentIndex > 0 && arr[currentIndex - 1].length > 1)
) {
return total + currentValue;
} else {
return total + " " + currentValue;
}
};


const concatSingleChars = (title) => {
const arrTitle = title.split(" ");
return arrTitle.reduce(reducer);
};


const convertCase = (str) => {
const s = toTitle(str);
return concatSingleChars(s);
};


const tests = [
"colName",
"This_Is_A_title",
"And_How_About_thisOne",
"MaryHadALittleLamb",
"employeeID",
"N B A Finals",
"N B A Finals in L A",
"I Love L A"
];


const titles = tests.map((test) => {
return convertCase(test);
});


console.log(titles);

If you're like me and had a camelCase value such as:

thisIsMyCamelCaseValue where the first letter is lowercased

function fromCamelCase(value) {
const spaced = value.replace(/([a-z])([A-Z])/g, '$1 $2');
return spaced.charAt(0).toUpperCase() + spaced.slice(1);
}