匹配#tags 但不匹配十六进制代码的正则表达式
Regex expression that matches #tags but not hex codes
我正在尝试在 JavaScript 中编写一个正则表达式,它将匹配任何字符串,只要它以 space 开头,然后是 octothorpe(#),然后是字符。但是,我希望这个表达式排除十六进制代码。
我有这个捕获#tags的表达式:
/([\s]#[^<\s]+)/g
并且我有一个表达式以 (#xxxxxx) 格式捕获十六进制代码,我的大型程序将接收它们:
/(#[0-9a-fA-F]{6,6}\b)/g
但我不知道如何将它们放在一起,以便我最终得到第一个表达式描述的匹配项而不是第二个表达式描述的匹配项。
我想在一个正则表达式语句中做所有事情。如果这不可能,我想知道一种使用正则表达式和 JavaScript 函数的组合来获取所有以 # 开头的非十六进制字符串的方法。如果有帮助,我正在使用 jQuery 和 Backbone.js。
加分:
这有什么区别:
/(#[0-9a-fA-F]{6,6}\b)/g
还有这个:
/(#[0-9a-fA-F]{6}\b)/g
我一直在使用 https://regex101.com 编写和测试我的表达式,两者的结果似乎相同。
您可以在第一个正则表达式中使用第二个正则表达式作为否定前瞻 ((?!
):
(?:\s|^)(#(?![\da-fA-F]{6}\b)[^<\s]+)
我在开始时添加了立即用散列开始字符串的可能性,而不需要 space。
注意:{6,6}
确实与快捷方式完全相同:{6}
。如 regular-expressions.info 所述:
There's an additional quantifier that allows you to specify how many times a token can be repeated.
The syntax is {min,max}, where min is zero or a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches. [...] Omitting both the comma and max tells the engine to repeat the token exactly min times.
我正在尝试在 JavaScript 中编写一个正则表达式,它将匹配任何字符串,只要它以 space 开头,然后是 octothorpe(#),然后是字符。但是,我希望这个表达式排除十六进制代码。
我有这个捕获#tags的表达式:
/([\s]#[^<\s]+)/g
并且我有一个表达式以 (#xxxxxx) 格式捕获十六进制代码,我的大型程序将接收它们:
/(#[0-9a-fA-F]{6,6}\b)/g
但我不知道如何将它们放在一起,以便我最终得到第一个表达式描述的匹配项而不是第二个表达式描述的匹配项。
我想在一个正则表达式语句中做所有事情。如果这不可能,我想知道一种使用正则表达式和 JavaScript 函数的组合来获取所有以 # 开头的非十六进制字符串的方法。如果有帮助,我正在使用 jQuery 和 Backbone.js。
加分:
这有什么区别:
/(#[0-9a-fA-F]{6,6}\b)/g
还有这个:
/(#[0-9a-fA-F]{6}\b)/g
我一直在使用 https://regex101.com 编写和测试我的表达式,两者的结果似乎相同。
您可以在第一个正则表达式中使用第二个正则表达式作为否定前瞻 ((?!
):
(?:\s|^)(#(?![\da-fA-F]{6}\b)[^<\s]+)
我在开始时添加了立即用散列开始字符串的可能性,而不需要 space。
注意:{6,6}
确实与快捷方式完全相同:{6}
。如 regular-expressions.info 所述:
There's an additional quantifier that allows you to specify how many times a token can be repeated.
The syntax is {min,max}, where min is zero or a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches. [...] Omitting both the comma and max tells the engine to repeat the token exactly min times.