我有以下正则表达式:
我需要正则表达式在保持相同规则的情况下,加上不分割连接符“-”连接的单词。
console.log("Test #words 100-200-300".toLowerCase().match(/(?:\B#)?\w+/g))
从上面可以看到它正在分割“100-200-300”。我希望它忽略“-”并保持完整的单词,如下所示:
--> ["test", "#words", "100-200-300"]
我需要正则表达式在保持相同规则的情况下,加上不分割连接符“-”连接的单词。
(?:\B#)?\w+(?:-\w+)*
或者没有\B
的#?\w+(?:-\w+)*
一样重复匹配-
和\w+
。https://regex101.com/r/YRg5My/1 - The fourth birdstr.split(" ")
?对于下划线分隔的单词,您会怎么做? - dvo.toLowerCase()
,因为\w
也匹配它。尝试console.log("Test #words 100-200-300".match(/#?\w+(?:-\w+)*/g));
参见 https://rextester.com/RSVR49477 - The fourth bird