正则表达式不';在Javascript中无法正常工作?
关于在象棋pgn字符串中的“comments”周围插入括号,我得到了极好的帮助,完成了一个分别匹配移动列表和注释的正则表达式 以下是当前的正则表达式:正则表达式不';在Javascript中无法正常工作?,javascript,php,regex,Javascript,Php,Regex,关于在象棋pgn字符串中的“comments”周围插入括号,我得到了极好的帮助,完成了一个分别匹配移动列表和注释的正则表达式 以下是当前的正则表达式: ((?:\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2}(?:\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[
((?:\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2}(?:\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2})?\s?[()]?\s?[()]?\s?)+)|((?:(?!\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2}).)+)
三个捕获组是:
CleanPgnText
函数。我最感兴趣的是while
,但是如果还有什么不对劲的地方,我将非常感谢您的帮助
function CleanPgnText(pgn) {
var pgnTextEdited = '';
var str;
var pgnInputTextArea = document.getElementById("pgnTextArea");
var pgnOutputArea = document.getElementById("pgnOutputText");
str = pgnInputTextArea.value;
str = str.replace(/\[/g,"("); //sometimes he uses [ incorrectly for variations
str = str.replace(/\]/g,")");
str = str.replace(/[\n¬]*/g,""); // remove newlines and that weird character that MS Word sticks in
str = str.replace(/\s{2,}/g," "); // turn more than one space into one space
while ( str =~ /((?:\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2}(?:\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2})?\s?[()]?\s?[()]?\s?)+)|((?:(?!\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2})[^\)\(])+)|((?:\)\s\())/g ) {
if ($1.length > 0) { //
pgnTextEdited += $1;
}
else if ($2.length > 0) {
pgnTextEdited += '{' + $2 + '}';
}
else if ($3.length > 0) {
pgnTextEdited += $3;
}
}
pgnOutputArea.innerHTML = pgnTextEdited;
}
问题2:关于while
语句中的=~
while ( str =~
我从我的帮助代码中获得了=~
,但它是用Perl编写的。我不太明白=~
操作符是如何工作的。我可以在Javascript中使用相同的操作符,还是应该使用其他操作符
问题3:我能用吗。我说的时候,我的长度是多少
if ($1.length > 0)
看看第一个捕获组是否匹配
提前感谢您的帮助。
(如果regex101链接对您不起作用,您可以从原始线程中获取一个示例pgn进行测试)。我更正了您的javascript代码,并获得以下内容:
=~
,但可能我错了。使用JavaScript,您可以使用以下内容循环匹配:(为什么它的格式不像代码??)
模式=/myregexp/;
while((match=pattern.exec(mytext))!=null){
//做点什么
}null
。您可以使用上面的match
变量以及类似match[2]
is matching group 2的索引来寻址组我在看你的新正则表达式,它不太正确。尽管它看起来与@wumpz的JS代码一起工作,
你不能只在评论部分排除父母的意见,因为你是
仅匹配字符串文字
)(
序列(在捕获组3中)。这可能会将parenths排除在匹配之外,因为它不会成为新闻字符串的一部分
这是构造的。不太可能,因为移动与父项匹配 要解决这个问题,只需从注释中排除'(`),然后首先匹配它(组1)。
此外,我还留下了一些关于新正则表达式所做更改的注释。
试试看。我认为@wumpz值得称赞
# /(\)\s*\()|((?:\s?[()]?\s?[()]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2}(?:\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2})?\s?[()]?\s?[()]?\s?)+)|((?:(?!\s?[()]?\s?[()]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-])(?!\)\s*\()[\S\s])+)/
( \) \s* \( ) # (1), 'Special Comment' configuration (must match first)
| # OR,
( # (2 start), 'Moves' configuration
(?:
\s?
[()]? \s? [()]?
\s?
[0-9]{1,3} \.{1,3}
\s
[NBRQK]? [a-h1-8]? x? [a-hO] [1-8-] [O-]{0,3} [!?+#=]{0,2} [NBRQ]?
[!?+#]{0,2}
(?:
\s
[NBRQK]? [a-h1-8]? x? [a-hO] [1-8-] [O-]{0,3} [!?+#=]{0,2} [NBRQ]? [!?+#]{0,2}
)?
\s?
[()]? \s? [()]?
\s?
)+
) # (2 end)
| # OR,
( # (3 start), 'Normal Comment' configuration
(?:
(?! # Not the 'Moves configuration'
\s?
[()]? \s? [()]?
\s?
[0-9]{1,3} \.{1,3}
\s
[NBRQK]? [a-h1-8]? x? [a-hO] [1-8-]
# ----
# Next line is not needed
# because all its items are
# optional
# ----
### [O-]{0,3} [!?+#=]{0,2} [NBRQ]? [!?+#]{0,2} <- not needed
)
### [^)(] <- replaced by '[\S\s]' below
# ----
# The above line is replaced by any char.
# because it excludes all ()'s and is not appropriate
(?! \) \s* \( ) # Also, Not the 'Sspecial comment' configuration
[\S\s] # Consume any char
)+
) # (3 end)
在Perl程序中运行此命令,输出为:
{哈巴罗夫斯克是俄罗斯远东地区的首府。我16岁的对手是一位有前途的当地神童。现在他是一位非常强大的FM,民盟评级为2437,生活在……美国,也是一个小世界。}1.e4 c5 2.Nf3 e6 3.c3 Nf6 4.e5 Nd5.d4 cxd4 6.cxd4 d6 7.Nc3 Nc6 8.Bd3!?Nxc3 9.bxc3 dxe5 10.dxe5 Qa5 11.O-O Be7 12.Qb3 Nxe5 13.Nxe5 Qxe5 14.Bb5+Kf8 15.Ba3 QCRD7 16.Rad1 g6 17.c4!Bxa3 18.Qxa3+Kg7 19.Rd6 20.Bd7 21.Bc4 Bc6 22.RFG3 23{最终,通过准确、可靠的战术,黑人已经巩固,而白人仍然保持着一些压力,并对棋子进行了一些补偿。}24.h4{在这样的位置上进行一次典型的游行,没有什么比这更好的。}24…h5?!(24…h6{将是一个更谨慎的回应。}({但最好的防御是}24…Rd6!25.cd6 Qa5)25.Qe5+Kh7 26.Bd3{非常自然}26…Kh6?({失踪}26…Ba4!27.Qxh5+Kg7 28.Qe5+Kg8!{现在黑人有很多自己的威胁。白人必须在}29.h5!Bxd1 30.h6 f6 31.Qxf6 Bh5 32.Qxe6+Kh7 33.Bxg6+Bxg6 34.Qxg6+Kh8 35.Qf6+{现在,在26…Kh6之后,一切都准备好了决定性的打击。})27.Qf6!Kh7({27…Rxd6 28.cxd6 Rxd6{由于}29.Qh8#)28.g4!hxg4 29.h5 Rxd6 30.cxd6 Rxd6 31.hxg6+Kg8 32.g7{这个棋子在现在结束之前是至关重要的因素。任何其他动作,怀特都会输。}32…Qd8{对抗Qh6和Qh8的唯一防御手段是猎杀或女王。}33.Qh6 f5 34.Rd2!!{这个想法是白色的车不能再被支票接受了。主教将很容易被粉碎的Bxf5或Bc4解开。d文件上的黑色别针是一种错觉!事实上是黑色的车被钉住了,不能离开d文件。}34…Bd5({最好的尝试-关闭d文件,保护更多e6兵。没有帮助。}34…Rd7 35.Bf5 ef5 36.Qh8 Kf7 37.Rd7)({但也许最好的实际机会是}34…g3!{现在是}35.Bxf5{因为}35…gxf2+36.Kh2 f1=N+!37.Kh3 Bg2+!38.Rxg2 Rd3+!39.Bxd3 Qxd3+{具有惊人的永久性}40.Kh4 Qe4+41.Rg4 Qh1+42.KF5+43.Kf6+44。。。
function CleanPgnText(pgn) {
var pgnTextEdited = '';
var str;
var pgnOutputArea = document.getElementById("pgnOutputText");
str = pgn;
str = str.replace(/\[/g, "("); //sometimes he uses [ incorrectly for variations
str = str.replace(/\]/g, ")");
str = str.replace(/[\n¬]*/g, ""); // remove newlines and that weird character that MS Word sticks in
str = str.replace(/\s{2,}/g, " "); // turn more than one space into one space
//Start regexp processing
var pattern = /(\)\s*\()|((?:\s?[()]?\s?[()]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2}(?:\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2})?\s?[()]?\s?[()]?\s?)+)|((?:(?!\s?[()]?\s?[()]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-])(?!\)\s*\()[\S\s])+)/g;
while ((match = pattern.exec(str)) != null) {
if (match[1] != null) { // Special Comment configuration, don't add '{}'
pgnTextEdited += match[1];
} else if (match[2] != null) { // Moves configuration
pgnTextEdited += match[2];
} else if (match[3] != null) { // Normal Comment configuration, add '{}'
pgnTextEdited += '{' + match[3] + '}';
}
}
//end regexp processing
pgnOutputArea.innerHTML = pgnTextEdited;
}