Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.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,我刚刚使用,来创建下面的正则表达式 ([^,]*?)=(.*?)(?(?=, )(?:, )|(?:$))(?(?=[^,]*?=)(?:(?=[^,]*?=))|(?:$)) 它似乎非常适合我的用例,即获取逗号分隔的键和值,同时在值中保留逗号 问题是,我想在Node.js(JavaScript)中使用这个正则表达式,但在regex101中编写整个正则表达式时,我将它设置为PCRE(PHP) 看起来JavaScript不支持条件Lookaheads((?(?=…)()|()) 有没有办法让它在

我刚刚使用,来创建下面的正则表达式

([^,]*?)=(.*?)(?(?=, )(?:, )|(?:$))(?(?=[^,]*?=)(?:(?=[^,]*?=))|(?:$))
它似乎非常适合我的用例,即获取逗号分隔的键和值,同时在值中保留逗号

问题是,我想在Node.js(JavaScript)中使用这个正则表达式,但在regex101中编写整个正则表达式时,我将它设置为PCRE(PHP)

看起来JavaScript不支持条件Lookaheads(
(?(?=…)()|()

有没有办法让它在JavaScript中工作


示例:

2场比赛

第一组:
id
,第二组:
1

第一组:
name
,第二组:
bob

id=1, name=bob

3场比赛

第1组:
id
,第2组:
2

第1组:
类型
,第2组:
存储

id=2, type=store, description=Hardwood Store
第1组:
说明
,第2组:
硬木店

id=2, type=store, description=Hardwood Store

4场比赛

第1组:
id
,第2组:
4

第一组:
类型
,第二组:
道路

第一组:
名称
,第二组:
世界上最长的道路名称,永远是宇宙中最长的道路名称

第一组:
builded
,第二组:
20190714

id=4, type=road, name=The longest road name, in the entire world, and universe, forever, built=20190714

3场比赛

第1组:
id
,第2组:
3

第一组:
类型
,第二组:
建筑

第1组:
builder
,第2组:
Random Name和其他人,在最终人员的帮助下

id=3, type=building, builder=Random Name, and Other Person, with help from Final Person

也许,这些表达式与您可能想要设计的表达式有些接近:

([^=\n\r]*)=\s*([^=\n\r]*)\s*(?:,|$)

不过我不确定


如果要探索/简化/修改该表达式,请在的右上面板中进行说明

const regex=/\s*([^=\n\r]*)=\s*([^=\n\r]*)\s*(?:,|$)/gm;
const str=`id=3,type=building,builder=Random Name,以及其他人员,由最终人员提供帮助
id=4,type=road,name=世界上最长的道路名称,而universe,forever,builded=20190714
id=2,类型=store,描述=Hardwood store
id=1,name=bob
`;
让m;
while((m=regex.exec(str))!==null){
//这是避免具有零宽度匹配的无限循环所必需的
if(m.index==regex.lastIndex){
regex.lastIndex++;
}
//可以通过'm`-变量访问结果。
m、 forEach((匹配,组索引)=>{
log(`Found match,group${groupIndex}:${match}`);
});
}
您可以使用

/([^,=\s][^,=]*)=(.*?)(?=(?:,\s*)?[^,=]*=|$)/g

详细信息

  • ([^,=\s][^,=]*)
    -第1组:
    • [^,=\s]
      -除
      =
      和空白以外的字符
    • [^,=]*
      -除
      =
      以外的零个或多个字符
  • =
    -a
    =
    字符
  • (.*)
    -第2组:除换行符以外的任何零个或多个字符,尽可能少
  • (?=(?:,\s*)?[^,=]*=|$)
    -一种正向前瞻,需要可选的
    和0+空格序列,然后是0+字符,而不是
    =
    ,然后是当前位置右侧的
    =
    或字符串结尾
JS演示:

var strs=['id=1,name=bob','id=2,type=store,description=Hardwood store','id=4,type=road,name=世界上最长的路名,universe,forever,builded=20190714','id=3,type=building,builder=Random name,以及其他人,在最终用户的帮助下']
变量rx=/([^,=\s][^,=]*)=(.*)(=(?:,\s*)?[^,=]*=$)/g;
用于(STR的var s){
log(“字符串:”,s);
var-m;
while(m=rx.exec){
console.log(m[1],m[2])
}

}
还有另一种方法

\s*([^,=]*?)\s*=\s*(((?:(?![^,=]*=)[\s\s])*)(?=[=,]|$)

可读版本

 \s* 
 ( [^,=]*? )                   # (1), Key
 \s* = \s*                     #       =
 (                             # (2 start), Value
      (?:
           (?! [^,=]* = )
           [\S\s] 
      )*
 )                             # (2 end)
 (?= [=,] | $ )

终极PCRE版本

\s*([^,=]*?)\s*=\s*((?:(?!\s*[^,=]*=)[\s\s])*(

\s*#Wsp修剪
([^,=]*?)#(1),键
\s*=\s*#Wsp修剪=Wsp修剪
(#(2开始),值
(?:
(?!\s*[^,=]*=)
[\S\S]
)*

(?这非常有效!感谢您的详细解释。唯一的问题是,我意识到一些值中也有=符号。因此,现在我必须找出一种方法,确保在=,或它是值的一部分之前有一个逗号。例如
id=1,url=https://test.com/hello/world?name=bob,random=5
.3与她匹配e、
id
&
1
url
&
https://test.com/hello/world?name=bob
random
&
5
@CharlieFish我认为逗号和空格是可选的。如果不是,请使用
 \s*                           # Wsp trim
 ( [^,=]*? )                   # (1), Key
 \s* = \s*                     # Wsp trim =  Wsp trim
 (                             # (2 start), Value
      (?:
           (?! \s* [^,=]* = )
           [\S\s] 
      )*
      (?<! [,\s] )                  # Wsp trim
 )                             # (2 end)
 \s*                           # Wsp trim
 (?= [=,\s] | $ )              # Field seperator