[英]Split string by whitespace, keeping escaped characters
I'm trying to split a string by its whitespace, but keep any escaped whitespace characters.我正在尝试按其空格拆分字符串,但保留任何转义的空格字符。
Example line:示例行:
/etc/space\ in\ folder\ name/files /mnt/files none defaults 0 0
I want to split that up by the whitespace (tab or spaces) yet keep the escaped whitespace characters ( \
) as one item.我想通过空格(制表符或空格)将其拆分,但将转义的空格字符(
\
)保留为一项。
So in this case it would split it into 6 items.因此,在这种情况下,它会将其拆分为 6 个项目。
How can I achieve this?我怎样才能做到这一点?
A regular expression can match any escaped character, or any character that isn't a space:正则表达式可以匹配任何转义字符或任何非空格字符:
const str = String.raw`/etc/space\ in\ folder\ name/files /mnt/files none defaults 0 0`; console.log( str.match(/(?:\\.|\S)+/g) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.