dtc99987 2014-03-21 11:58
浏览 50
已采纳

正则表达式在Javascript中无法正常工作?

In an earlier thread about inserting brackets around "comments" in a chess pgn-like string, I got excellent help finishing a regex that matches move lists and comments separately.

Here is the current regex:

((?:\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}).)+)

The three capture groups are:

  1. "e4 e5 2. f4 exf4 3.Nf3" etc -- i.e. lists of moves
  2. "Blah blah blah" -- i.e. "comments"
  3. comment ") (" comment -- i.e. close and begin parens, when a chess variation with a comment at the end "completes", and another chess variation with a comment at the beginning "starts"

In action here: http://regex101.com/r/dQ9lY5

Everything works correctly for "Your regular expression in" PCRE(PHP): it matches all three groups correctly. When I switch to "Your regular expression in" Javascript, however, it matches everything as Capture Group 1. Is there something in my regex that isn't supported by the Javascript regex engine? I tried to research this, but haven't been able to solve it. There is so much information on this topic, and I've already spent hours and hours.

I know one solution is to use the regex as-is, and pass it to PHP through AJAX, etc, but I don't know how to do that yet (it's on my list to learn).

Question 1: But I am also very curious about what it is in this regex that doesn't work on the Javascript regex engine.

Also, here is my Javascript CleanPgnText function. I am most interested in the while, but if anything else seems wrong, I would appreciate any help.

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(/[
¬]*/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;
}

Question 2: Regarding the =~ in the while statement

while ( str =~

I got the =~ from helpful code in my original thread, but it was written in Perl. I don't quite understand how the =~ operator works. Can I use this same operator in Javascript, or should I be using something else?

Question 3: Can I use .length the way I am, when I say

if ($1.length > 0) 

to see if the first capture group had a match?

Thank you in advance for any help. (If the regex101 link doesn't work for you, you can get a sample pgn to test on from the original thread).

  • 写回答

2条回答 默认 最新

  • doufang8282 2014-03-21 13:58
    关注

    I corrected your javascript code and got the following:

    http://jsfiddle.net/ZXG2H/

    1. Personally I think the matching (group) problems are related to http://regex101.com/. Your expression works definitly in JavaScript (see the fiddle) and in Java (with escaping corrections). I minimalized your JavaScript slightly and used the pgn data from a parameter not a text input.

    2. I am not aware that =~ is available in JavaScript, but maybe I am wrong. Using JavaScript you loop through the matches using something like: (Why does it not format like code???)

      pattern=/myregexp/; while ((match=pattern.exec(mytext))!=null) { //do something }

    3. If no match is found for a group it returns null. You adress the groups by using the match variable from above with an index like match[2] is matching group 2.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算