简体   繁体   English

Javascript正则表达式-没有尾随空格时如何匹配

[英]Javascript regex - how to match when no trailing spaces

In this regex I'm trying to extract the list of tables from a SQL select statement. 在此正则表达式中,我尝试从SQL select语句中提取表列表。 It works fine with one exception, if the statement ends with the table name there is no match. 如果该语句以表名结尾,则没有匹配项,但有一个例外,它可以正常工作。 For example: 例如:

Given he regex: 鉴于他正则表达式:

(?:from|join)\s+(.*?)(?:\)|\s+(?:where|on|inner|outer|full|left|right|join|\s*$))

and given this string: 并给出以下字符串:

"select xxxx  from table1 t1, table2, table3 t3 "  (note the last space)

The match is: 匹配为:

"table1 t1, table2, table3 t3"   

But given this string: 但鉴于此字符串:

"select xxxx  from table1 t1, table2, table3 t3"  (without last space)

There's no match. 没有比赛。 How to make this work? 如何使这项工作?

RegEx isn't very good at this, as it's a lot more complicated than it appears: 正则表达式不是很好,因为它比看起来复杂得多:

  • What if they use LEFT/RIGHT INNER/OUTER/CROSS/MERGE/NATURAL joins instead of the a,b syntax? 如果他们使用LEFT / RIGHT INNER / OUTER / CROSS / MERGE / NATURAL连接而不是a,b语法怎么办? The a,b syntax should be avoided anyway. 无论如何,应避免使用a,b语法。
  • What about nested queries? 嵌套查询呢?
  • What if there is no table (selecting a constant) 如果没有表怎么办(选择一个常数)
  • What about line breaks and other whitespace formatting? 换行符和其他空白格式呢?
  • Alias names? 别名?

What you can do is an sql parser, and there is a good one Hrer . 您可以做的是一个SQL解析器,并且有一个很好的Hrer

See more answers in this post . 在这篇文章中查看更多答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM