2016-03-29 20 views
1

私は、スクリプトを使ってテレプロンプトを自動的にフォーマットします。すべてを大文字にすることになっています(例外はあります)。しかし、角か角かっこだけでなく、かっこの中に何かを残すべきです。正規表現の選択が機能しない

<script> 
String.prototype.smartUpperCase = function(){ 
    var pattern = /(.*?[a-z][A-Z])(.*)/g; 
    if(pattern.test(this)){ 
     return this.replace(pattern,function(t,a,b){ 
      return a+b.toUpperCase(); 
     }); 
    } 
    else{ 
     return this.toUpperCase(); 
    } 
} 
String.prototype.regexEscape = function(){ return this.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); } 
String.prototype.removeBrackets = function(){ return this.replace(/[\<\>\[\]\(\)]/g, ""); } 
String.prototype.format = function(returnValNoShow){ 
    text = this; 
    orig = text; // for use in multi-line regex pattern 
    text = text.replace(/(\w+)/g,function(t,w){ return w.smartUpperCase(); }); // smart uppercase everything 
    text = text.replace(/\d{1,2}[st|nd|rd|th]{2}/gi, function(m){ return m.toLowerCase(); }); // for dates (1st, 2nd, etc. will be lowecase) 
    // complicated regex -> find anything inside <>, [],() and inject the original string back in 
    var pattern = /.*(?=[^\<]*\>|[^\[]*\]|[^\(]*\)).*/g; 
    text = text.replace(pattern, function(match){ 
     console.log(match); 
     if(match==""){ return ""; } 
     var pattern2 = new RegExp(".*(?="+match.regexEscape()+").*", "gi"); 
     //console.log(orig.match(pattern2)); 
     return orig.match(pattern2)[0]; 
    }); 

    text = text.replace(/\&/g, "AND"); // switch & for and 

    text = text.replace(/ +/g, " "); // replace multiple spaces with one 
    text = text.replace(/\n{3,}/g, "\n\n"); // replace 3+ line breaks with two 
    text = text.replace(/\}\n{2,}/g, "}\n"); // don't allow empty line after name 
    text = text.replace(/\n{2,}-+\n{2,}/g, "\n---\n"); // don't allow blank line between break (---) 

    text = text.replace(/\n /g, "\n").replace(/ \n/g, "\n"); // trim() each line 

    text = text.trim(); // trim whitespace on ends 
    return text; 
} 
function f() { 
    document.getElementById("in").value = document.getElementById("in").value.format(); 
} 
</script> 

そして十分に単純であるHTML::予想通り、これは働く時間の

<textarea id="in" rows="40" cols="80">{NAME} 
THANKS ____ AND ____. AS WE REPORTED LAST MONDAY, BATMAN VS SUPERMAN: DAWN OF JUSTICE CAME OUT THIS PAST WEEKEND AND IT SET SOME BOX OFFICE RECORDS. 

{NAME} 
(DDR) That's right ____. 'Batman v Superman' took huge $170 million at the box office. Audiences flocked to see the pairing of Batman (Ben Affleck) versus Superman (Henry Cavill) in the DC Comics film, which also introduced Wonder Woman (Gal Gadot). 

{NAME} 
IT'S THE BIGGEST MARCH OPENING WEEKEND EVER, EVEN BEATING 2012'S THE HUNGER GAMES' WHO BROUGHT IN $152.5 MILLION. 

{NAME} 
IN OTHER NEWS - SYRACUSE IS THE FIRST 10 SEED TO MAKE IT TO THE FINAL FOUR. 

(ad lib) 
</textarea> 
<br/> 
<input type="button" onclick="f()" value="Format"/> 

99%ここで

私が作成したコードです。しかし、第2段落で示されているように、時には何もしません。

(テキストエリア内のテキストが既にフォーマットを経験してきた)

答えて

1

最初の問題は、あなたの「括弧の中のものを見つける」正規表現ということである。

var pattern = /.*(?=[^\<]*\>|[^\[]*\]|[^\(]*\)).*/g; //wrong 

文字列全体にマッチさ :パターンの適切な部分はゼロ幅の "先読み"アサーションで囲まれ、ブール値yes/noとしてのみ機能します。あなた(も.*を取り除くことにより、文字列の残りの部分を食べていない間)、彼らは適切に交換できるように、積極的に消費するパターンでこれらの配列と一致する必要があります。この問題が再び発生した

var pattern = /(\([^\(]*\)|\{[^\{]*\}|\[[^\[]*\])/g; 

これは再びmatchを見るために先読み

var pattern2 = new RegExp(".*(?="+match.regexEscape()+").*", "gi"); //wrong 

が、全く一致するものがいた場合には、それができるでしょうので、それは、.*ワイルドカードシーケンスで囲まれています:あなたは、元のテキストを照合することで、あなたの置換パターンを構築します全体の文字列。あなたはそれがしたいようにそれが動作する、置き換えるないときthis demo shows your code working as intended ...

var pattern2 = new RegExp(match.regexEscape(), "gi") 

:に変更。

+0

私はそれと同じくらい簡単なことを逃したとは思いません。ありがとう、トン! – jhpratt

+0

あなたは歓迎です! – sweaver2112

関連する問題