我一直在尝试编写一个正则表达式来匹配星号、波浪号、破折号和方括号。
我拥有的:
const str = "The] quick [brown] fox **jumps** over ~~the~~ lazy dog --- in the [woods";
console.log(str.match(/[^\]][^\[\]]*\]?|\]/g));
// [
// "The]",
// " quick ",
// "[brown]",
// " fox **jumps** over ~~the~~ lazy dog --- in the ",
// "[woods"
// ];
我想要的是:
[
"The]",
" quick ",
"[brown]",
" fox ",
"**jumps**",
" over ",
"~~the~~",
" lazy dog ",
"---",
" in the ",
"[woods"
];
编辑:
字符串的更多示例/组合是:
"The] quick brown fox jumps [over] the lazy [dog"
// [ "The]", " quick brown fox jumps ", "[over]", " the lazy ", "[dog" ]
"The~~ quick brown fox jumps [over] the lazy **dog"
// [ "The~~", " quick brown fox jumps ", "[over]", " the lazy ", "**dog" ]
编辑2:
我知道这很疯狂,但是:
"The special~~ quick brown fox jumps [over the] lazy **dog on** a **Sunday night."
// [ "The special~~", " quick brown fox jumps ", "[over the]", " lazy ", "**dog on**", " a ", "**Sunday night" ]
米脂
心有法竹
相关分类