2016-03-29 21 views
1

Teleprompter için komut dosyasını otomatik olarak biçimlendiren bir komut dosyası var. Herşeyi (belli istisnalar dışında) sermayeleştirmesi gerekiyor. Bununla birlikte, parantezin yanı sıra tek başına köşeli veya köşeli parantez içinde de bir şey bırakmalıdır.Regex seçimi çalışmıyor

<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> 

Ve yeterince basittir HTML: beklendiği gibi çalıştığını zamanın

<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% Burada

oluşturduğum kodudur. Ancak, ikinci paragrafta gösterildiği gibi, bazen hiçbir şey yapmaz.

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

tüm dizeyi eşleşen edilir:

cevap

1

ilk sorun senin normal ifade "parantez içinde şeyler bulmak" olduğunu (textarea metin zaten biçimlendirme geçirdi) : Desenin ilgili kısmı, sıfır genişliği olan ve yalnızca bir boole evet/hayır işlevi gören bir "lookahead" iddiasına eklenmiştir.

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

Bu sorun tekrar ne zaman karşılaşıldığında: (aynı zamanda .* kurtularak dize kalanını yemek değil iken) düzgün değiştirilebilir böylece, aktif bir alıcı desende bu dizileri eşleşmesi gerekir Bu yine match görmek ileri bakar

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

, ama hiç bir eşleşme varsa, o olacak yani, .* joker dizileriyle çevrili: orijinal metinde karşı maç için ile yedek deseni oluşturmak tüm dize Bu değiştirin: Eğer ... this demo shows your code working as intended bunu istediğiniz gibi değiştiririm şimdi zaman

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

işe yarıyor.

+0

Bunun gibi basit bir şeyi kaçırdığımı düşünemiyorum. Teşekkürler bir ton! – jhpratt

+0

kabul edilir dostum! – sweaver2112