【发布时间】:2021-01-02 21:08:58
【问题描述】:
我正在尝试匹配仅包含此字符类中的字符的单词:[A-z'\\/%],排除以下情况:
- 它们介于
<和>之间 - 它们在
[和]之间 - 它们在
{和}之间
所以,假设我有这个有趣的字符串:
[beginning]<start>How's {the} /weather (\\today%?)[end]
我需要匹配以下字符串:
[ "How's", "/weather", "\\today%" ]
我尝试过使用这种模式:
/[A-z'/\\%]*(?![^{]*})(?![^\[]*\])(?![^<]*>)/gm
但由于某种原因,它匹配:
[ "[beginning]", "", "How's", "", "", "", "/weather", "", "", "\\today%", "", "", "[end]", "" ]
我不确定为什么我的模式允许 [ 和 ] 之间的内容,因为我使用了 (?![^\[]*\]),并且类似的方法似乎适用于 not 匹配 {these cases} 和<these cases>。我也不确定为什么它匹配所有的空字符串。
有什么智慧吗? :)
【问题讨论】:
标签: javascript regex string match