简体   繁体   English

Javascript正则表达式获取不在单引号或双引号中的字符串列表

[英]Javascript Regex get list of string not in single or double quotes

Guy I have a list of string. 伙计,我有一个字符串列表。

Select 
id AS "cusId ",
name as 'cusName', gendar as ' Gendar.',
isPaid as " is'Paid ", total, remarks FROM

I need a regex that returns: 我需要一个返回的正则表达式:

Select
id
name
gendar
isPaid
total
remarks
FROM

And also ignore comma and 'AS' keyword. 并且也忽略逗号和'AS'关键字。

So far from PHP I can use preg_match_all('/(?<![\\S"])([^"\\'\\s]+)(?![\\S"])/') and filter all query keywords later on, but came to JavaScript there is no lookbehind in regex. 到目前为止,我可以使用preg_match_all('/(?<![\\S"])([^"\\'\\s]+)(?![\\S"])/')并稍后过滤所有查询关键字上,但是来到JavaScript时,正则表达式中没有任何隐藏的内容。

DISCLAIMER: The solution below is by no means a generic solution for parsing arbitrary SQL queries. 免责声明:以下解决方案绝不是解析任意SQL查询的通用解决方案。 To parse arbitrary SQL queries, you need to build or use an existing one. 要解析任意SQL查询,您需要构建或使用现有的SQL查询。 See also How to parse / tokenize an SQL statement in Node.js . 另请参阅如何在Node.js中解析/标记化SQL语句

So, taking into account your specific input strings , you can use a regex that will match what you do not need, and then will capture what you need: 因此,考虑到您的特定输入字符串 ,您可以使用匹配您不需要的正则表达式,然后捕获您需要的正则表达式:

/"[^"]*"|'[^']*'|\s+AS\s+|\s*((?:(?!\sAS\s)[^,\s])+)/gi

See the regex demo 正则表达式演示

Explanation : 说明

  • "[^"]*" - match a double-quoted substring that has no " inside (replace with "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" if you need to support escaped " inside) "[^"]*" -匹配内部没有"的双引号子字符串(如果需要,请替换为"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"支持逃脱"里面"
  • | - or - 要么
  • '[^']*' - match single-quoted substring having no ' inside (replace with '[^'\\\\]*(?:\\\\.[^'\\\\]*)*' if you need to support escaped ' inside) '[^']*' -匹配内部没有'单引号子字符串(替换为'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'如果需要支持转义'里面)
  • | - or - 要么
  • \\s+AS\\s+ - "AS" word inside 1+ whitespaces \\s+AS\\s+ -1个空格内的“ AS”字
  • | - or - 要么
  • \\s* - 0+ whitespaces \\s* -0+空格
  • ((?:(?!\\sAS\\s)[^,\\s])+) - Group 1 capturing one or more symbols other than , and whitespace (see [^,\\s])+ ) that are not starting a sequence of a whitespace + AS + whitespace. ((?:(?!\\sAS\\s)[^,\\s])+) -组1捕获一个和多个除,和空格以外的符号(请参见[^,\\s])+ ),这些符号未开始空格+ AS +空格的顺序。 It matches any text that is not space + AS + space . 它匹配不是space + AS + space任何文本。

JS demo: JS演示:

 var re = /"[^"]*"|'[^']*'|\\s+AS\\s+|\\s*((?:(?!\\sAS\\s)[^,\\s])+)/gi; var str = 'Select id AS "cusId ", name as \\'cusName\\', gendar as \\' Gendar.\\', isPaid as " is\\'Paid " total , datetime FROM'; var res = []; while ((m = re.exec(str)) !== null) { if (m[1]) { res.push(m[1]); // Add the Capture group 1 to the resulting array } } document.body.innerHTML = "<pre>" + JSON.stringify(res, 0, 4) + "</pre>"; 

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

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