简体   繁体   English

在javascript中使用正则表达式解析数组语法

[英]Parsing array syntax using regex in javascript

I found the answer for this here but it's in php. 我在这里找到了答案,但是它在php中。

I would like to match an array like [123, "hehe", "lala"] but only if the array syntax is correct. 我想匹配[123, "hehe", "lala"]这样的数组,但[123, "hehe", "lala"]是数组语法正确。

I made this regex /\\["?.+"?(?:,"?.+"?)*\\]/ . 我做了这个正则表达式/\\["?.+"?(?:,"?.+"?)*\\]/

The problem is that if the input is [123, "hehe, "lala"] , the regex match, but the syntax is incorrect. 问题是,如果输入为[123, "hehe, "lala"] ,则正则表达式匹配,但是语法不正确。

How can I make it only match if the array syntax is correct? 如何仅在数组语法正确的情况下使其匹配?

My problem is making the second " required when the first " is matched. 我的问题是使第二个"匹配第一个"时需要。

Edit: I'm only trying to do it only with strings and numbers inside the array. 编辑:我只是想只用数组中的字符串和数字来做到这一点。

You can try this regex: /\\[((\\d+|"([^"]|\\\\")*?")\\s*,?\\s*)*(?<!,)\\]/ 您可以尝试使用此正则表达式: /\\[((\\d+|"([^"]|\\\\")*?")\\s*,?\\s*)*(?<!,)\\]/

Each item should either 每个项目应

"([^"]|\\\\")*?" : start and end with ", containing anything but ". :以“开头”和“结尾”,除“之外”。 If " is contained it should be escaped (\\"). 如果包含“,则应将其转义(\\“)。

\\d+ : a number \\d+ :一个数字

After each item should be 在每个项目之后

\\s*,?\\s* : a comma with any number of spaces before or after. \\s*,?\\s* :逗号,前后带有任意数量的空格。

And before the closing bracket should not be a comma: (?<!,) 并且在右括号之前不应使用逗号: (?<!,)

Demo: https://regex101.com/r/jRAQUc/1 演示: https : //regex101.com/r/jRAQUc/1

You must have two (or more) separate expressions (using the | operator) in order to do that. 为此,您必须具有两个(或更多个)单独的表达式(使用|运算符)。

So it would be something like this: 所以会是这样的:

/\[\s*("[^"]*"|[0-9]+)(\s*,\s*("[^"]*"|[0-9]+))*\s*\]/

(You may also want to use ^ at the start and $ at the end to make sure nothing else appears before/after the array: /^...snip...$/ to match the string from start to finish.) (您可能还想在开头使用^ ,在结尾使用$ ,以确保在数组之前/之后没有其他内容:/ /^...snip...$/ $ / /^...snip...$/匹配字符串。)

If you need floating point numbers with exponents, add a period and the 'e' character: [0-9.eE]+ (which is why I did not use \\d+ because only digits are allowed in that case.) To make sure a number is valid, it's much more complicated, obviously (sign, exponent with/without sign, digits only before or after the decimal point...) 如果您需要带指数的浮点数,请添加句点和'e'字符: [0-9.eE]+ (这就是为什么我不使用\\d+原因,因为在这种情况下只允许使用数字)。一个数字是有效的,显然要复杂得多(符号,带/不带符号的指数,仅在小数点之前或之后的数字...)

You could also support single quoted strings. 您还可以支持单引号字符串。 That too is a separate expression: '[^']*' . 那也是一个单独的表达式: '[^']*'

You may want to allow spaces before and after the brackets too (start: /^\\s*\\[... and end: ...\\]\\s*$/ ). 您可能也想在方括号之前和之后都留有空格(开始: /^\\s*\\[...和结束: ...\\]\\s*$/ )。

Finally, if you want to really support JavaScript strings you would need to add support for the backslash. 最后,如果您想真正支持JavaScript字符串,则需要添加对反斜杠的支持。 Something like this: ("([^"]|\\\\.)*") . 像这样的东西:( ("([^"]|\\\\.)*")

Note 注意

Your .+ expression would match " and , too and without the ^ and $ an array as follow matches your expression just fine: 您的.+表达式将匹配"并且,并且也没有^$数组,如下所示将与您的表达式完全匹配:

This Array ["test", 123, true, "this"] Here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM