Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何检查正则表达式组是否为空?_Node.js_Regex - Fatal编程技术网

Node.js 如何检查正则表达式组是否为空?

Node.js 如何检查正则表达式组是否为空?,node.js,regex,Node.js,Regex,我需要用node.js替换文件中的一些字符串 const myTest = { files: filePath, from: /test1="(.*?)"\s+(test2="(.*?)")?\s?(test3="(.*?)")?/g, to: 'r1-$1 r2-$2 r3-$3' } 我的正则表达式获取test1、test2和test3的组。但有时test2和test3并不存在 如何检查$2和$3是否为空/为空?当.replace

我需要用node.js替换文件中的一些字符串

const myTest = {
files: filePath,
from: /test1="(.*?)"\s+(test2="(.*?)")?\s?(test3="(.*?)")?/g,
to: 'r1-$1 r2-$2 r3-$3'
}
我的正则表达式获取test1、test2和test3的组。但有时test2和test3并不存在


如何检查$2和$3是否为空/为空?当
.replace()
replacement
参数是不支持条件逻辑的字符串时,我们是否可以对regex(“$2!=null?my grp-$2:”)执行空测试。您必须使用函数回调。请参见Try
from:/test1=“([^”]*)”\s+(?:test2=“([^”]*)”)(?:\s*test3=“([^”]*)”)/g、
to:([uux,x,y,z)=>`r1-${x`+(y?`r2-${y}:'')+(z?`r3-${z}:'')
嗨,我看你可能已经看到我的建议了,你有时间测试一下吗?