如何忽略正则表达式主题字符串中的空格?
使用正则表达式模式搜索匹配项时,是否有一种简单的方法可以忽略目标字符串中的空格?例如,如果我的搜索是“cats”,我希望“cats”或“ca ts”匹配。我无法事先删除空格,因为我需要找到匹配的开始和结束索引(包括任何空格),以便突出显示该匹配,并且出于格式化目的,需要存在任何空格。
Is there a simple way to ignore the white space in a target string when searching for matches using a regular expression pattern? For example, if my search is for "cats", I would want "c ats" or "ca ts" to match. I can't strip out the whitespace beforehand because I need to find the begin and end index of the match (including any whitespace) in order to highlight that match and any whitespace needs to be there for formatting purposes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以在正则表达式中的每个其他字符之间粘贴可选的空白字符
\s*
。虽然被授予,但它会变得有点冗长。/cats/
-> /c\s*a\s*t\s*s/You can stick optional whitespace characters
\s*
in between every other character in your regex. Although granted, it will get a bit lengthy./cats/
->/c\s*a\s*t\s*s/
虽然接受的答案在技术上是正确的,但如果可能的话,更实用的方法是从正则表达式和搜索字符串中删除空格。
如果您想搜索“我的猫”,而不是:
只需执行:
警告:您不能通过仅用空字符串替换所有空格来在正则表达式上自动执行此操作,因为它们可能出现在否定中或以其他方式使您的正则表达式无效的。
While the accepted answer is technically correct, a more practical approach, if possible, is to just strip whitespace out of both the regular expression and the search string.
If you want to search for "my cats", instead of:
Just do:
Warning: You can't automate this on the regular expression by just replacing all spaces with empty strings because they may occur in a negation or otherwise make your regular expression invalid.
针对史蒂文对萨姆·杜菲尔的回答的评论
这应该可以解决问题:
请参阅 此页 匹配所有不同的“cats”变体。
您还可以使用 条件 解决此问题,但正则表达式的 javascript 风格不支持它们。
Addressing Steven's comment to Sam Dufel's answer
This should do the trick:
See this page for all the different variations of 'cats' that this matches.
You can also solve this using conditionals, but they are not supported in the javascript flavor of regex.
您可以将
\s*
放在搜索字符串中的每个字符之间,因此如果您要查找 cat,则可以使用c\s*a\s*t\s*s\s*s
它很长,但您当然可以动态构建字符串。
您可以在这里看到它的工作原理: http://www.rubular.com/r/zzWwvppSpE
You could put
\s*
inbetween every character in your search string so if you were looking for cat you would usec\s*a\s*t\s*s\s*s
It's long but you could build the string dynamically of course.
You can see it working here: http://www.rubular.com/r/zzWwvppSpE
如果您只想允许空格,那么
应该这样做。 如果您还想在
bobcats
或catsupcats
,也可以使用制表符,请使用删除
\b
锚点代码>.If you only want to allow spaces, then
should do it. To also allow tabs, use
Remove the
\b
anchors if you also want to findcats
within words likebobcats
orcatsup
.这种方法可用于自动化
(以下示例性解决方案是用Python编写的,尽管显然它可以移植到任何语言):
您可以预先去除空格并保存非空格字符的位置,以便稍后可以使用它们来找出匹配的字符串边界位置原始字符串如下所示:
如果你想更进一步,你可以构造匹配对象并返回它,所以这个助手的使用会更方便。
当然这个函数的性能也可以优化,这个例子只是为了展示解决方案的路径。
This approach can be used to automate this
(the following exemplary solution is in python, although obviously it can be ported to any language):
you can strip the whitespace beforehand AND save the positions of non-whitespace characters so you can use them later to find out the matched string boundary positions in the original string like the following:
If you want to go further you can construct the match object and return it instead, so the use of this helper will be more handy.
And the performance of this function can of course also be optimized, this example is just to show the path to a solution.
如果您传递动态值(例如数组循环中的“当前值”)作为正则表达式测试值,则接受的答案将不起作用。如果没有一些非常难看的正则表达式,您将无法输入可选的空格。
因此,Konrad Hoffner 的解决方案在这种情况下更好,因为它会去除 regest 和 test 字符串中的空格。测试将按照两者都没有空格的方式进行。
The accepted answer will not work if and when you are passing a dynamic value (such as "current value" in an array loop) as the regex test value. You would not be able to input the optional white spaces without getting some really ugly regex.
Konrad Hoffner's solution is therefore better in such cases as it will strip both the regest and test string of whitespace. The test will be conducted as though both have no whitespace.