Javascript 如何在Node.js中使用我的PHP正则表达式?

Javascript 如何在Node.js中使用我的PHP正则表达式?,javascript,php,regex,node.js,preg-match-all,Javascript,Php,Regex,Node.js,Preg Match All,我在PHP中有一个正则表达式,但当我将它移植到Node.js时,我得到的输出与我从PHP中得到的输出不同,但我认为这是因为我不知道如何使PREG_SET_ORDER在Node.js中工作 示例文本: INPUT - Each line represents a line inside a text file. ------------------------------------------------------------------------------------- "!?Tex

我在PHP中有一个正则表达式,但当我将它移植到Node.js时,我得到的输出与我从PHP中得到的输出不同,但我认为这是因为我不知道如何使PREG_SET_ORDER在Node.js中工作


示例文本:

INPUT - Each line represents a line inside a text file. 
-------------------------------------------------------------------------------------
"!?Text" (1234)                                         1234-4321
"#1 Text" (1234)                                        1234-????
#2 Text (1234) {Some text (#1.1)}                       1234
Text (1234)                                             1234
Some Other Text: More Text here 1234-4321 (1234) (V)    1234
preg_match_all("/^((.*?) *\((\d+)\))(?: *\{((.*?) *\((.+?)\)) *\})?/m",$data,$r, PREG_SET_ORDER);
$i = 0;
foreach($r as $a) {
    array_splice($a, 0, 2);
    if(count($a) > 2) {
        array_splice($a, 2, 1);
    }
    print_r($a);
}
var regex = /^((.*?) *\((\d+)\))(?: *\{((.*?) *\((.+?)\)) *\})?/mg
var result = data.toString().match(regex);
console.log(result);
Array
(
    [0] => "!?Text"
    [1] => 1234
)
Array
(
    [0] => "#1 Text"
    [1] => 1234
)
Array
(
    [0] => #2 Text
    [1] => 1234
    [2] => Some text
    [3] => #1.1
)
Array
(
    [0] => Text
    [1] => 1234
)
Array
(
    [0] => Some Other Text: More Text here 1234-4321
    [1] => 1234
)
[ '"!?Text" (1234)',
  '"#1 Text" (1234)',
  '#2 Text (1234) {Some text (#1.1)}',
  'Text (1234)',
  'Some Other Text: More Text here 1234-4321 (1234)' ]
PHP:

INPUT - Each line represents a line inside a text file. 
-------------------------------------------------------------------------------------
"!?Text" (1234)                                         1234-4321
"#1 Text" (1234)                                        1234-????
#2 Text (1234) {Some text (#1.1)}                       1234
Text (1234)                                             1234
Some Other Text: More Text here 1234-4321 (1234) (V)    1234
preg_match_all("/^((.*?) *\((\d+)\))(?: *\{((.*?) *\((.+?)\)) *\})?/m",$data,$r, PREG_SET_ORDER);
$i = 0;
foreach($r as $a) {
    array_splice($a, 0, 2);
    if(count($a) > 2) {
        array_splice($a, 2, 1);
    }
    print_r($a);
}
var regex = /^((.*?) *\((\d+)\))(?: *\{((.*?) *\((.+?)\)) *\})?/mg
var result = data.toString().match(regex);
console.log(result);
Array
(
    [0] => "!?Text"
    [1] => 1234
)
Array
(
    [0] => "#1 Text"
    [1] => 1234
)
Array
(
    [0] => #2 Text
    [1] => 1234
    [2] => Some text
    [3] => #1.1
)
Array
(
    [0] => Text
    [1] => 1234
)
Array
(
    [0] => Some Other Text: More Text here 1234-4321
    [1] => 1234
)
[ '"!?Text" (1234)',
  '"#1 Text" (1234)',
  '#2 Text (1234) {Some text (#1.1)}',
  'Text (1234)',
  'Some Other Text: More Text here 1234-4321 (1234)' ]
Node.js:

INPUT - Each line represents a line inside a text file. 
-------------------------------------------------------------------------------------
"!?Text" (1234)                                         1234-4321
"#1 Text" (1234)                                        1234-????
#2 Text (1234) {Some text (#1.1)}                       1234
Text (1234)                                             1234
Some Other Text: More Text here 1234-4321 (1234) (V)    1234
preg_match_all("/^((.*?) *\((\d+)\))(?: *\{((.*?) *\((.+?)\)) *\})?/m",$data,$r, PREG_SET_ORDER);
$i = 0;
foreach($r as $a) {
    array_splice($a, 0, 2);
    if(count($a) > 2) {
        array_splice($a, 2, 1);
    }
    print_r($a);
}
var regex = /^((.*?) *\((\d+)\))(?: *\{((.*?) *\((.+?)\)) *\})?/mg
var result = data.toString().match(regex);
console.log(result);
Array
(
    [0] => "!?Text"
    [1] => 1234
)
Array
(
    [0] => "#1 Text"
    [1] => 1234
)
Array
(
    [0] => #2 Text
    [1] => 1234
    [2] => Some text
    [3] => #1.1
)
Array
(
    [0] => Text
    [1] => 1234
)
Array
(
    [0] => Some Other Text: More Text here 1234-4321
    [1] => 1234
)
[ '"!?Text" (1234)',
  '"#1 Text" (1234)',
  '#2 Text (1234) {Some text (#1.1)}',
  'Text (1234)',
  'Some Other Text: More Text here 1234-4321 (1234)' ]
PHP(输出):

INPUT - Each line represents a line inside a text file. 
-------------------------------------------------------------------------------------
"!?Text" (1234)                                         1234-4321
"#1 Text" (1234)                                        1234-????
#2 Text (1234) {Some text (#1.1)}                       1234
Text (1234)                                             1234
Some Other Text: More Text here 1234-4321 (1234) (V)    1234
preg_match_all("/^((.*?) *\((\d+)\))(?: *\{((.*?) *\((.+?)\)) *\})?/m",$data,$r, PREG_SET_ORDER);
$i = 0;
foreach($r as $a) {
    array_splice($a, 0, 2);
    if(count($a) > 2) {
        array_splice($a, 2, 1);
    }
    print_r($a);
}
var regex = /^((.*?) *\((\d+)\))(?: *\{((.*?) *\((.+?)\)) *\})?/mg
var result = data.toString().match(regex);
console.log(result);
Array
(
    [0] => "!?Text"
    [1] => 1234
)
Array
(
    [0] => "#1 Text"
    [1] => 1234
)
Array
(
    [0] => #2 Text
    [1] => 1234
    [2] => Some text
    [3] => #1.1
)
Array
(
    [0] => Text
    [1] => 1234
)
Array
(
    [0] => Some Other Text: More Text here 1234-4321
    [1] => 1234
)
[ '"!?Text" (1234)',
  '"#1 Text" (1234)',
  '#2 Text (1234) {Some text (#1.1)}',
  'Text (1234)',
  'Some Other Text: More Text here 1234-4321 (1234)' ]
Node.js(输出):

INPUT - Each line represents a line inside a text file. 
-------------------------------------------------------------------------------------
"!?Text" (1234)                                         1234-4321
"#1 Text" (1234)                                        1234-????
#2 Text (1234) {Some text (#1.1)}                       1234
Text (1234)                                             1234
Some Other Text: More Text here 1234-4321 (1234) (V)    1234
preg_match_all("/^((.*?) *\((\d+)\))(?: *\{((.*?) *\((.+?)\)) *\})?/m",$data,$r, PREG_SET_ORDER);
$i = 0;
foreach($r as $a) {
    array_splice($a, 0, 2);
    if(count($a) > 2) {
        array_splice($a, 2, 1);
    }
    print_r($a);
}
var regex = /^((.*?) *\((\d+)\))(?: *\{((.*?) *\((.+?)\)) *\})?/mg
var result = data.toString().match(regex);
console.log(result);
Array
(
    [0] => "!?Text"
    [1] => 1234
)
Array
(
    [0] => "#1 Text"
    [1] => 1234
)
Array
(
    [0] => #2 Text
    [1] => 1234
    [2] => Some text
    [3] => #1.1
)
Array
(
    [0] => Text
    [1] => 1234
)
Array
(
    [0] => Some Other Text: More Text here 1234-4321
    [1] => 1234
)
[ '"!?Text" (1234)',
  '"#1 Text" (1234)',
  '#2 Text (1234) {Some text (#1.1)}',
  'Text (1234)',
  'Some Other Text: More Text here 1234-4321 (1234)' ]
我设法让它像这样工作:


这场比赛似乎很艰苦。为什么不按行分割并尝试分别匹配每一行?您是否确定数据填充正确,或者toString()函数工作正常?我似乎从函数中获得了一些匹配项,但如何在node.js中使PREG_SET_ORDER工作?