Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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
Javascript 使用变量检查字符串格式_Javascript_Regex - Fatal编程技术网

Javascript 使用变量检查字符串格式

Javascript 使用变量检查字符串格式,javascript,regex,Javascript,Regex,我想检查一个有效的路径,路径的某些部分可以是任意的。路径具有以下格式,可更改部分称为变量_1和变量_2。我怎么能检查这个 sftp:// name[:variable_1]‌@host:port[/variable_2]‌/file 将需要更具体的变量格式: new RegExp(/sftp:\/\/ name\[:variable_1\]‌@host:port\[\/variable_2\]‌\/file/) 将需要更具体的变量格式: new RegExp(/sftp:\/\/ name\

我想检查一个有效的路径,路径的某些部分可以是任意的。路径具有以下格式,可更改部分称为变量_1和变量_2。我怎么能检查这个

sftp:// name[:variable_1]‌@host:port[/variable_2]‌/file

将需要更具体的变量格式:

new RegExp(/sftp:\/\/ name\[:variable_1\]‌@host:port\[\/variable_2\]‌\/file/)

将需要更具体的变量格式:

new RegExp(/sftp:\/\/ name\[:variable_1\]‌@host:port\[\/variable_2\]‌\/file/)

您可以尝试以下操作:

sftp:\/\/ name:(\w+)@host:port\/(\w+)\/file
\w+
将匹配变量_1和变量_2


(我不知道)“名字”之前的空白是否是键入,但我保留了它)

你可以尝试如下:

sftp:\/\/ name:(\w+)@host:port\/(\w+)\/file
\w+
将匹配变量_1和变量_2


(我不知道)“名字”之前的空白是否是一个键入,但我保留了它)

你尝试过任何正则表达式吗?用“<代码> >代码>替换变量部分来允许任何东西,或者用<代码> \W*<代码>替换它们,或者可能是一些字符类,如<代码> [^ @ ] *<代码>和<代码> [^ ]。]*根据您的要求,我建议您只需执行
新建URL(“…”)
并从中获取属性(即
URL=newURL(“…”)
然后您就可以获取
URL.username
URL.password
URL.hostname
URL.port
URL.pathname
),但是。。。显然,它对
sftp://
不起作用,因为它只是将
sftp:
读作协议,其余部分全部读作路径名……是否有您尝试过的正则表达式?只需将变量部分替换为
*
以允许任何内容,或将其替换为
\w*
或可能是一些字符类,如
[^@]*
[^\]*
根据您的要求,我想建议您只需执行
新URL(“…”)
并从中获取属性(即
URL=newURL(“…”);
然后您就可以获取
URL.username
URL.password
URL.hostname
URL.port
URL.pathname
),但是。。。显然,它对
sftp://
不起作用,因为它只是将
sftp:
读取为协议,其余部分读取为路径名…regexAbov.exec(“sftp://name:variable_1@主机:port/variable_2/file”),返回null,或者我做错了什么吗?regexAbov.exec(“sftp://name:variable_1@主机:port/variable_2/file”),返回null,还是我做错了什么?