2017-06-06 49 views
1

基本上我想這樣做:有沒有一種優雅的方式將變量名稱中的單詞提取到數組中?

我得到一個字符串"myVariableName"並把它變成:["my", "variable", "name"]

我試着這樣做使用正則表達式,但我得到在我結束數組很多undefineds的好像。在這種情況下,變量名稱的兩種可能情況是駝峯案和上蛇案。

const matchVariableNames = /(\b[a-z]+)|([A-Z][a-z]+)|(\b[A-Z]+)|(_[A-Z]+)/g; 
const variableName = 'myVariable'; 
let words = []; 
let regexMatches; 

while (regexMatches = matchVariableNames.exec(variableName)) { 
    regexMatches.forEach((match) => { 
    words.push(match); 
    }); 
}; 

輸出:

["my", "my", undefined, undefined, undefined, "Variable", undefined, "Variable", undefined, undefined] 
undefined 
+0

' 「myVariableName」 .match(/((:^ | [AZ])[AZ] +)/ G?)'技術上的一些非字母字符作爲VAR名接受的,但是這取決於這可能就夠你var命名約定 –

+0

或更好的重複:https://stackoverflow.com/questions/13720256/javascript-regex-camelcase-to-sentence – mplungjan

+0

'console.log( 「myVariableName」 .replace(/^[az] | [AZ]/g,函數(v,i){ )' – mplungjan

回答

2

你可以用用大寫字母積極的前瞻。然後只映射小寫字母。

var string = 'myVariableName', 
 
    array = string.split(/(?=[A-Z])/).map(a => a.toLowerCase()); 
 
    
 
console.log(array);

2

我不認爲正則表達式是非常適合這一點。 (我不接受Regular expression to identify CamelCased words with leading uppercase letter作爲重複,因爲問題沒有要求正則表達式persé)。

我讀了你想要的方式來分析寫在駝峯字。立即我想象一下特殊情況,例如"DOMElement"PDFParser"。我期望輸出爲[ "DOM", "Element" ][ "PDF", "Parser" ]。我認爲我們可以編寫一個像那樣工作的函數。

function parseWordsFromCamelCaseString(string) { 
 
    var i, words, last_word_char, last_case, current_case; 
 
    words = []; 
 
    last_word_start = 0; 
 
    last_case = null; // false=lower, true=upper 
 

 
    for (i = 0; i < string.length; i += 1) { 
 
    current_case = string[i].toUpperCase() === string[i]; 
 
    if (last_case !== null && current_case !== last_case) { 
 
     if (current_case === true) { // lowercase to uppercase transition 
 
     words.push(string.substring(last_word_start, i)); 
 
     last_word_start = i; 
 
     } else { // uppercase to lowercase transition 
 
     if (last_word_start < i - 1) { 
 
      words.push(string.substring(last_word_start, i - 1)); 
 
     } 
 
     last_word_start = i - 1; 
 
     } 
 
    } 
 
    last_case = current_case; 
 
    } 
 
    if (last_word_start < i - 1) { 
 
    words.push(string.substring(last_word_start, i)); 
 
    } 
 
    return words; 
 
} 
 

 
console.log(parseWordsFromCamelCaseString("fooBar")); 
 
console.log(parseWordsFromCamelCaseString("parseWordsFromCamelCaseString")); 
 
console.log(parseWordsFromCamelCaseString("DOMElement")); 
 
console.log(parseWordsFromCamelCaseString("fooDOMElement"));

輸出

["foo", "Bar"] 
["parse", "Words", "From", "Camel", "Case", "String"] 
["DOM", "Element"] 
["foo", "DOM", "Element"] 

它也適用於"foo"(任何情況下的過渡)和""(空字符串)。

+0

你是否會識別[javascript-regex-camelcase-to-sentence](https://stackoverflow.com/questions/13720256/javascript-regex-camelcase-to-sentence)作爲重複呢? – mplungjan

+0

@mplungjan我不明白爲什麼。沒有非正則表達式答案。他們都沒有正確處理我概述的(不那麼)特殊情況。 – Halcyon

-1

我創造了這個:

x = 'myVariableName'; 
const isUpper = e => (e === e.toUpperCase()); 
[].reduce.call(x, (words, letter, i) => { 
    if(isUpper(letter)) words.push([letter]); 
    else { 
    let length = words.length-1, 
     word = words[length]; 
    words[length] = word+letter; 
    } 
    return words; 
}, [[]]); 

返回["my", "Variable", "Name"]

是它非常優雅?我們可以爭取,但肯定比您的RegExp更多。 :P

+0

它像輸入'myPDFFile'一樣中斷輸入。它返回'[「my」,[「p」],[「D」],[「F」],「File」]' – Halcyon

相關問題