如何在 Node.js 中使用我的 PHP 正则表达式
How to use my PHP regular expression in Node.js?
我在PHP中有一个正则表达式,但是当我将其移植到Node.js时,我得到的输出与我从PHP获得的输出不同,但我认为这是因为我真的不知道如何在Node.js中PREG_SET_ORDER工作。
示例文本:
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
.PHP:
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);
}
节点.js:
var regex = /^((.*?) *'(('d+)'))(?: *'{((.*?) *'((.+?)')) *'})?/mg
var result = data.toString().match(regex);
console.log(result);
PHP(输出):
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
)
节点.js(输出):
[ '"!?Text" (1234)',
'"#1 Text" (1234)',
'#2 Text (1234) {Some text (#1.1)}',
'Text (1234)',
'Some Other Text: More Text here 1234-4321 (1234)' ]
我设法让它像这样工作:
function data_to_array(data) {
var regex = '^((.*?) *''((''d+)''))(?: *''{((.*?) *''((.+?)'')) *''})?';
var Regex = new RegExp(regex, 'mg');
var Matches = data.match(Regex);
matchesArray = new Array();
for (var i in Matches) {
ngRegex = new RegExp(regex);
ngMatches = Matches[i].match(ngRegex);
ngMatches.splice(0, 2);
if(ngMatches.length > 2) {
ngMatches.splice(2, 1);
}
matchesArray.push(ngMatches);
}
return matchesArray;
}
var output = data_to_array(data.toString());
console.log(output);
相关文章:
- 在我的php模型中用javascript更改一个html类
- 如何通过Javascript调用我的PHP文件
- 我的 PHP 网址不会重定向
- 为什么我的PHP数组没有作为数组发布到JavaScript
- 我希望我的php/html网站上的JavaScript能够实时更新
- 为什么我的php数组只返回最近/最后发布的活动
- 如何保护我的php、jquery和ajax请求免受黑客攻击
- 我如何通过JavaScript连接到我的PHP程序所在的服务器
- 我的 AJAX 表单没有发送到我的 PHP
- 如何正确访问我的PHP文件?直接从JS或通过“;主“;PHP
- 为什么我的 php 文件不能从我的 JavaScript 运行
- 为什么我的 php echo 不起作用
- 为什么我的 php 连接到 mysql 服务器不起作用
- AngularJS与我的PHP API:获得值的工厂(async:false need alternative)
- 为什么我的PHP函数没有由jQuery $.get执行
- 不明白为什么我不用我的脚本更改我的 PHP 变量
- 如何在我的php注释框中实时替换带有表情符号的文本
- 现在我已经将内容发布到我的 PHP 脚本中,我该如何获取它
- 为什么 Javascript 事件处理程序会更改我的 PHP 变量
- 我的 PHP 无法接收我通过 ajax 发送的“帖子”变量