我正在制作一个 JS“命令行”模拟器。
我有正则表达式:/([^\s"]+)|"([^\s"]+)"/g。我想匹配单个单词,例如echo, wyświetl, jd923h90asd8。另外,我想匹配“字符串文字”——比如"this is a string"or "f82h3 23fhn aj293 dgja3 xcn32"。
我match在输入字符串上使用方法来获取所有匹配的数组。但问题是:当 Regexp 匹配“字符串文字”并将字符串返回到数组时,该字符串包含双引号。我不想要双引号,但问题是 - 为什么 Regexp 包含双引号?在 Regexp 中,引号从组""中排除。()为什么 Regexp 包含所有内容?
编辑:
var re = /([^\s"]+)|"([^\s"]+)"/g;
var process = function (text) {
return execute(text.match(re));
}
var execute = function (arr) {
console.log(arr);
try {
//... apply a function with arguments...
} catch (e) {
error(arr[0]+": wrong function");
return "";
}
}
对于输入echo abc "abc def" "ghi"Regexp 返回数组["echo", "abc", "abc", "def", ""ghi""]。我想做一个正则表达式,从那个输入中返回["echo", "abc", "abc def", "ghi"].