简体   繁体   English

javascript:如何使用空格分割此命令行字符串,但args包含空格

[英]javascript: how can I split this command line string using spaces but args contain spaces

the command line string I am trying to parse is as such: 我试图解析的命令行字符串是这样的:

   screenshotz [ 'li[title="about us"] a', 3,2 ] 
div.react-bs-container-body tr > td:nth-child(1)

I am expecting the following 3 items in a list: 我期望列表中包含以下3个项目:

    [`screenshotz`,`['li[title="about us"] a', 3,2]`,
 `div.react-bs tr > td:nth-child(1)`]

I am doing a command.split(/[Az] /g) , however, I end up with much more than 3, it takes account into every single space inside the second and last argument. 我正在执行command.split(/[Az] /g) ,但是最后得到的结果远远超过3,它考虑了第二个参数和最后一个参数中的每个空格。

Essentially I need to allow just about any type of characters, however one clear rule is that a user can submit without the [ ... ] characters: 本质上,我几乎需要允许任何类型的字符,但是一个明确的规则是,用户可以不使用[ ... ]字符来提交:

screenshot div.react-bs-container-body tr > td:nth-child(1)

which should return [screenshot, div.react-bs....] 应该返回[screenshot, div.react-bs....]

It always follows this rule, the [...] cannot come after the css selectors. [...]始终遵循此规则,css选择器之后不能出现[...]

How can I make split a string using the spaces on the "outside" and not inside the individual argument parameter in a command? 如何使用命令“外部”而不是单个参数内部的空格分割字符串?

So it shouldn't split a space in an argument parameter string. 因此,不应在参数参数字符串中分隔空格。 for instance that css has a ton of space but it should all be one arg param. 例如,css有很多空间,但都应该是一个arg参数。

Your regular expression is causing the string to be split on any alpha characters followed by space. 您的正则表达式导致字符串被拆分为任何字母字符,后跟空格。 You need to break down the command line string into its components: 您需要将命令行字符串分解为以下组件:

  1. screenshot at the beginning of the string, followed by a space. screenshot位于字符串的开头,后跟一个空格。
  2. Optionally, anything contained within square brackets [...] 可选地,方括号中包含的所有内容[...]
  3. Another, optional space, then everything else until the end of the string. 另一个可选空格,然后其他所有内容,直到字符串结尾。

Each of these components, grouped in RegEx, can be represented by: 在RegEx中分组的每个组件都可以表示为:

  1. (screenshot)␠
  2. (\\[.+\\])?
  3. ␠?(.+)

    (screenshot).+?([.+]) ?(.+) (屏幕截图)。+?([。+])?(。+)

I am assuming the z in screenshotz is a typo. 我假设screenshotzz是一个错字。

Using split results in blank results at the start and end of the array, instead, I recommend using matchAll : 在数组的开头和结尾使用split结果会导致结果空白,我建议使用matchAll

command = `screenshot [ 'li[title="about us"] a', 3,2 ]
div.react-bs-container-body tr > td:nth-child(1)`
Array.from(command.matchAll(/(screenshot) (\[.+\]) ?(.+)/sg))

The result is: 结果是:

Array(4)
0: "screenshot [ 'li[title="about us"] a', 3,2 ] ↵div.react-bs-container-body tr > td:nth-child(1)"
1: "screenshot"
2: "[ 'li[title="about us"] a', 3,2 ]"
3: "↵div.react-bs-container-body tr > td:nth-child(1)"

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

相关问题 如何在javascript中的行空格/中断处拆分字符串 - How to split a string on line spaces/breaks in javascript 如何使用javascript确定字符串是否只包含空格? - How can I determine if a string only contains spaces, using javascript? 如何在Javascript和HTML中的空格上分割字符串? - How to split a string on the spaces in Javascript & HTML? 如何将字符串拆分为字符,但在JavaScript中保留空格 - How to split string into characters but to keep spaces in javascript 如何在 javascript 中用空格替换换行符/换行符? - How can I replace newlines/line breaks with spaces in javascript? 在javascript / jquery中,如何将字符串(具有不一致的空格)解析为数组 - in javascript / jquery how can i parse a string (with inconsistence spaces) into an array 如何避免生成的JavaScript字符串文字包含用于缩进的空格? - How to avoid generated JavaScript string literal to contain spaces used for indentation? 如何使用 JavaScript 从字符串中删除空格? - How to remove spaces from a string using JavaScript? 如何用空格和标点符号分割 javascript 字符串? - How do you split a javascript string by spaces and punctuation? 如何在保留空格的同时在空格和非字母数字字符上拆分 javascript 字符串 - How to split a javascript string on spaces and non alphanumeric characters while keeping spaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM