分割空格,避免使用双引号JS字符串:从'a'b' c’ d'至['a','''b \\' c","d"] [英] Split spaces avoiding double-quoted JS strings : from 'a "b \\" c" d ' to ['a','"b \\" c"','d']

查看:233
本文介绍了分割空格,避免使用双引号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"'
]

感谢这个问题:

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小提琴: http://jsfiddle.net/GwY8Y/1/ 您可以看到开头已被很好地解析,但最后一个参数已被拆分和存在错误.

Here is a JS-Fiddle : http://jsfiddle.net/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 \\' c","d"]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆