我有一个脚本,它自动为提词器格式化脚本。它应该把所有的东西都资本化(除了某些例外)。然而,它也应将任何东西置于角度或方括号之外,以及括号内。
下面是我创建的代码:
<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%的时间如预期的那样工作。然而,正如第二段所示,它有时什么也不做。
(文本区域中的文本已经通过格式设置)
发布于 2016-04-07 18:31:01
第一个问题是,您的“在括号中查找内容”正则表达式:
var pattern = /.*(?=[^\<]*\>|[^\[]*\]|[^\(]*\)).*/g; //wrong匹配整个字符串:模式的相关部分包含在“展望”断言中,该断言为零宽度,仅用作布尔值“是/否”。您需要在消费模式中主动匹配这些序列(同时也不要通过去掉.*来消耗其余的字符串),以便能够正确地替换它们:
var pattern = /(\([^\(]*\)|\{[^\{]*\}|\[[^\[]*\])/g;当您构建要与原始文本匹配的替换模式时,再次遇到此问题:
var pattern2 = new RegExp(".*(?="+match.regexEscape()+").*", "gi"); //wrong这再次展望了match,但是它被.*通配符序列包围了,所以如果有匹配,它将是整个字符串。将此更改为:
var pattern2 = new RegExp(match.regexEscape(), "gi")现在,当您进行替换时,它的工作方式就像您希望它.此演示展示了您的代码按预期工作。一样。
https://stackoverflow.com/questions/36287489
复制相似问题