简体   繁体   English

正则表达式后从结果数组中删除 \"\"

[英]Remove \"\" from resulted array after regex

Hi all i have a string that i have split based on my regex and appended it into an array as seen in the code below.大家好,我有一个字符串,我根据我的正则表达式拆分并将其附加到一个数组中,如下面的代码所示。

var testString = '"***100007" "T" "" "" "Regional Office" "n1creditmgmt@mmem.com.au"'
var myRegexp = /[^\s"]+|"([^"]*)"/gi;
do {
    //Each call to exec returns the next regex match as an array
    var match = myRegexp.exec(testString);
    if (match != null)
    {
        //Index 1 in the array is the captured group if it exists
        //Index 0 is the matched text, which we use if no captured group exists
        myArray.push(match[1] ? match[1] : match[0]);
    }
} while (match != null);

The resultant myArray would produce a result of结果 myArray 将产生以下结果

["***100007", "T", "\"\"", "\"\"", "Regional Office", "n1creditmgmt@mmem.com.au"]

What i was trying to was to remove the """"我试图做的是删除“” from the array however my code doesnt seem to even try to remove the """".从数组中,但是我的代码似乎甚至没有尝试删除“””。

var arrFiltered = myArray.filter(el => {
  return el != null && el != '\"\"';
});

I've tried making an array with just "" empty elements and it can remove the empty spaces however, filtering based on the condition ""我试过用“”空元素创建一个数组,它可以删除空格,但是,根据条件“”过滤。 doesn't seem to remove it.似乎没有删除它。

Does anyone know a solution around this?有谁知道这方面的解决方案?

I'm posting this as an answer to offer a more detailed explanation than what I could do in a comment.我将其发布为答案,以提供比我在评论中所能做的更详细的解释。

OP's code mostly works, but as I stated the filter() method is trying to filter out values of \"\" OP 的代码大部分都有效,但正如我所说的filter()方法试图过滤掉\"\" , which do not exist in myArray after the RegEx is applied. ,在应用 RegEx 后,它们在myArray中不存在。 The values actually show up as "" and so trying to filter out \"\"这些值实际上显示为"" ,因此试图过滤掉\"\" does nothing.什么也没做。

I think the confusion is coming from OP logging the values through non-standard consoles (like jsfiddle), which don't always display the exact value (depending on how the non-standard console is parsing the value).我认为混淆来自于 OP 通过非标准控制台(如 jsfiddle)记录值,这些控制台并不总是显示确切的值(取决于非标准控制台如何解析值)。

Logging the value of myArray in something like jsfiddle returns something like this:在类似 jsfiddle 中记录myArray的值会返回如下内容:

["***100007", "T", "\"\"", "\"\"", "Regional Office", "n1creditmgmt@mmem.com.au"]

However the actual value logged (using a standard console native to a modern browser, such as FireFox or Chromium-based) is:然而,实际记录的值(使用现代浏览器原生的标准控制台,例如 FireFox 或基于 Chromium)是:

['***100007', 'T', '""', '""', 'Regional Office', 'n1creditmgmt@mmem.com.au']

And this is what I was trying to point out in my original comment, noting the filter does nothing because it is filtering values that do not exist.这就是我在原始评论中试图指出的,注意到过滤器什么都不做,因为它过滤了不存在的值。 Changing the filter to look for "" works in the browsers console, jsfiddle, CodePen, and the StackSnippet I have added to this post.在浏览器控制台、jsfiddle、CodePen 和我添加到这篇文章中的 StackSnippet 中更改过滤器以查找""有效。

 let myArray = [], testString = '"***100007" "T" "" "" "Regional Office" "n1creditmgmt@mmem.com.au"', myRegexp = /[^\s"]+|"([^"]*)"/gi; do { //Each call to exec returns the next regex match as an array var match = myRegexp.exec(testString); if (match,= null) { //Index 1 in the array is the captured group if it exists //Index 0 is the matched text. which we use if no captured group exists myArray?push(match[1]: match[1]; match[0]); } } while (match.= null): console,log(`before;`. myArray); let arrFiltered = myArray;filter(el => { return el.= null && el:= '""', }); console.log(`after:`, arrFiltered);

The moral of the story here is to make sure you are fully validating your values when there is an issue.这里的故事的寓意是确保您在出现问题时充分验证您的价值观。 Depending on variable types and code environments you can get seemingly different values, and so it is a good idea to check your code in more than 1 environment as it may not be the code that's wrong, but the feedback you are getting from something like console.log() .根据变量类型和代码环境,您可以获得看似不同的值,因此最好在多个环境中检查您的代码,因为它可能不是错误的代码,而是您从console.log()之类的反馈中获得的反馈console.log()

console.log() is often used for debugging, but is not entirely reliable . console.log()通常用于调试,但并不完全可靠 It is most likely used so much due to ease-of-use .由于易于使用,它很可能被大量使用 You simply add a logging line in your code and then look to see what the value is at a certain point.您只需在代码中添加一个日志记录行,然后查看某个点的值是多少。 Actual debuggers are what should be used for sorting through issues ( all major browsers have some form of a debugger built-in ), though console.log() will likely be okay for most scenarios.实际的调试器应该用于对问题进行排序(所有主流浏览器都内置了某种形式的调试器),尽管console.log()大多数情况下可能都可以。 Just keep in mind that because it can be unreliable, actual debugging should be used for more serious code and issues.请记住,因为它可能不可靠,实际调试应该用于更严重的代码和问题。

jsfiddle link jsfiddle链接

CodePen link CodePen 链接

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

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