分割空格,避免使用双引号JS字符串:从'a'b' c’ d'至['a','''b \\' c","

编程入门 行业动态 更新时间:2024-10-23 04:59:06
本文介绍了分割空格,避免使用双引号JS字符串:从'a'b' c’ d'至['a','''b \\' c","d"]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在为自定义文件格式构建一个小型文本编辑器.我有一个GUI,但是我也实现了一个小的输出控制台.我想要实现的是添加一个非常基本的输入字段来执行一些命令并传递参数. 命令如下所示:

I am currently building a small text editor for a custom file format. I have a GUI, but I also implemented a small output console. What I want to achieve is to add a very basic input field to execute some commands and pass parameters. A command would look like :

compile test.json output.bin -location "Paris, France" -author "Charles \"Demurgos\""

我的问题是要得到一个包含以空格分隔的参数的数组,但是保留双引号部分,这可能是JSON.stringify生成的包含内部转义双引号的字符串.

My problem is to get an array containing the space-separated arguments, but preserving the double quoted parts which might be a string generated by JSON.stringify containing escaped double-quotes inside.

要清楚,上一个命令的预期数组是:

To be clear, the expected array for the previous command is :

[ 'compile', 'test.json', 'output.bin', '-location', '"Paris, France"', '-author', '"Charles \\"Demurgos\\""' ]

然后我可以遍历此数组,如果indexOf('"') == 0则应用JSON.parse以获得最终结果:

Then I can iterate over this array and apply a JSON.parse if indexOf('"') == 0 to get the final result :

[ 'compile', 'test.json', 'output.bin', '-location', 'Paris, France', '-author', 'Charles "Demurgos"' ]

感谢这个问题:使用逗号分割字符串,但使用Javascript忽略双引号中的逗号.如果参数不包含任何双引号,我就能得到所需的内容.这是我得到的正则表达式:

Thanks to this question : Split a string by commas but ignore commas within double-quotes using Javascript . I was able to get what I need if the arguments do NOT contain any double-quotes. Here is the regex i got :

/(".*?"|[^"\s]+)(?=\s*|\s*$)/g

但是,即使遇到转义,它也会在遇到双引号时退出当前参数.我如何适应此RegEx来处理转义引号或不引号?对于边缘情况,如果我提示action "windowsDirectory\\" otherArg,在这里反斜杠已经被转义了,所以即使在其后加双引号,也应退出该参数. 在以前的项目中,我一直试图避免这个问题,但是我觉得是时候该学习如何正确使用帐户底下的转义字符了.

But it exits the current parameter when it encounters a double-quote, even if it is escaped. How can I adapt this RegEx to take care about the escaped or not double quotes ? And what about edge cases if I prompt action "windowsDirectory\\" otherArg, here the backslash is already escaped so even if it's followed by a double quote, it should exit the argument. This a problem I was trying to avoid as long as possible during previous projects, but I feel it's time for me to learn how to properly take under-account escape characters.

这是一个JS小提琴: jsfiddle/GwY8Y/1/ 您可以看到开头已被很好地解析,但最后一个参数已被拆分和存在错误.

Here is a JS-Fiddle : jsfiddle/GwY8Y/1/ You can see that the beginning is well-parsed but the last arguments is split and bugs.

谢谢您的帮助.

推荐答案

此正则表达式将为您提供所需的字符串(请参见演示):

This regex will give you the strings you need (see demo):

"(?:\\"|\\\\|[^"])*"|\S+

像这样使用它:

your_array = subject.match(/"(?:\\"|\\\\|[^"])*"|\S+/g);

解释正则表达式

" # '"' (?: # group, but do not capture (0 or more times # (matching the most amount possible)): \\ # '\' " # '"' | # OR \\\\ # two backslashes | # OR [^"] # any character except: '"' )* # end of grouping " # '"' | # OR \S+ # non-whitespace (all but \n, \r, \t, \f, # and " ") (1 or more times (matching the # most amount possible))

更多推荐

分割空格,避免使用双引号JS字符串:从'a'b' c’ d'至['a','''b \

本文发布于:2023-11-29 17:24:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647083.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空格   字符串   双引号   JS   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!