2016-11-30 2 views
-2

im new student ...現在、私はこのregExpを取得する方法を見つけようとしています。まずは申し訳ありませんが、説明するのは難しいです。javascript reg exp

私は正規表現たい:

ABCD //which given true in exact sequence 

input : AOOBCOODOO or ACCCBCOODOO 
output: aOObcOOdOO or aCCCbcOOdOO //A,B,C,D in order get lower-cased 

input : AYYBTTCDDD , output : aYYbTTcdD ; 
input : ASRBB // return false no 'C' 'D' 
input : AABBCCDD , output : aAbBcCdD 

がtrueを返すと、 'A' 'B' 'C' 'D'、そして同じことが

を変更することはないであろう第二のアルファベットのための小文字になります意志

var rE = /A.*[B].*[C].*[D]/g; //so i can get exact-order for regex 
//which are A >> B >> C >> D 

はので、私は言葉を返すようにしたいが、exact alphabet:ここ

​​は私がしようとしているものです異なる(小文字)。

+0

'String.toLowerCase()'十分ではありませんだと思いますか? – nicovank

+0

@nicovankとすべてが小文字になりますか?私はちょうど正確なアルファベットがほしい、それはなぜ正規表現を使用しているのですか? –

+0

おそらく何らかのパーサーを書く必要があります – TheLostMind

答えて

0

私は、これは何が必要

function extract(input, words) { 
 
    // Any array or string "words" argument is acceptable 
 
    if(typeof words === 'string') { 
 
    // Lets convert words to array if it is single string 
 
    words = words.split(''); 
 
    } 
 
    
 
    // We only accept string inputs and array words 
 
    if(typeof input !== 'string' || !Array.isArray(words)) { 
 
    throw new SyntaxError('input not string or words not in expected format'); 
 
    } 
 
    
 
    // Lets create a regex which extracts all words and others 
 
    var reg = new RegExp(
 
    '^(.*?)' + 
 
    words.map(w => `(${w})`).join('(.*?)') + 
 
    '(.*)$', 
 
    'i' 
 
); 
 
    
 
    // If input matches then let replace it, otherwise it will return false 
 
    return reg.test(input) && input.replace(reg, function() { 
 
    // first argument is $0 (matched input) 
 
    // last couple arguments are index and input 
 
    
 
    // other arguments are groups 
 
    // Even indexed groups are our non matched parts 
 
    var args = Array.prototype.slice.call(arguments, 1, arguments.length - 2); 
 
    
 
    return args.map((a, idx) => ((idx % 2) && a.toLowerCase()) || a) 
 
     .join(''); 
 
    }); 
 
} 
 
              
 
console.log(extract('AABBBBBBCCDD', 'ABCD')) 
 
console.log(extract('ASRBB', 'ABCD')) 
 
console.log(extract('aAAaaBBbbbbBBcccDDddd', 'ABCD')) 
 
console.log(extract('HOWHJJHJHHABOUTKKHHOTHERS', ['HOW', 'ABOUT', 'OTHERS']));