如何在字符串中创建标志数组?(JavaScript)

如何在字符串中创建标志数组?(JavaScript),javascript,Javascript,如果我有这样一个字符串:hello here--flag test--anotherFlag 如何在该语句中获取标志(--something)数组,然后还获取不包含这些标志的字符串?另外请注意,这些标志不接受任何值,所以我想要--flag,而不是--flag测试 谢谢大家! 有一些字符串和数组方法使这项任务变得简单 const命令='hello there--flag test--anotherFlag'; const words=command.split(“”); console.log(

如果我有这样一个字符串:
hello here--flag test--anotherFlag

如何在该语句中获取标志(--something)数组,然后还获取不包含这些标志的字符串?另外请注意,这些标志不接受任何值,所以我想要--flag,而不是--flag测试


谢谢大家!

有一些字符串和数组方法使这项任务变得简单

const命令='hello there--flag test--anotherFlag';
const words=command.split(“”);
console.log(单词);//['hello'、'there'、'--flag'、'test'、'--anotherFlag']
const flags=words.filter(word=>word.startsWith('--');
console.log(标志);//['--flag','--anotherFlag']
const notFlags=words.filter(word=>!word.startsWith('--');
console.log(notFlags);//[‘你好’、‘那里’、‘测试’]
const stringOfNotFlags=notFlags.join(“”);

console.log(stringOfNotFlags);/''您好,这里是test'
有一些字符串和数组方法可以简化此任务

const命令='hello there--flag test--anotherFlag';
const words=command.split(“”);
console.log(单词);//['hello'、'there'、'--flag'、'test'、'--anotherFlag']
const flags=words.filter(word=>word.startsWith('--');
console.log(标志);//['--flag','--anotherFlag']
const notFlags=words.filter(word=>!word.startsWith('--');
console.log(notFlags);//[‘你好’、‘那里’、‘测试’]
const stringOfNotFlags=notFlags.join(“”);

console.log(stringOfNotFlags);/''您好,这里是测试“
我的建议是使用正则表达式

const text=“你好——标志测试——另一个标志”;
常量结果=[…text.matchAll(/--[a-zA-Z]*/g)];//匹配前面带有--

console.log(result)//正则表达式匹配单词数组
我的建议是使用正则表达式

const text=“你好——标志测试——另一个标志”;
常量结果=[…text.matchAll(/--[a-zA-Z]*/g)];//匹配前面带有--
console.log(result)//正则表达式匹配单词数组