私は、スクリプトを使ってテレプロンプトを自動的にフォーマットします。すべてを大文字にすることになっています(例外はあります)。しかし、角か角かっこだけでなく、かっこの中に何かを残すべきです。正規表現の選択が機能しない
<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段落で示されているように、時には何もしません。
(テキストエリア内のテキストが既にフォーマットを経験してきた)
私はそれと同じくらい簡単なことを逃したとは思いません。ありがとう、トン! – jhpratt
あなたは歓迎です! – sweaver2112