简体   繁体   中英

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

["***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 \"\" , which do not exist in myArray after the RegEx is applied. 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).

Logging the value of myArray in something like jsfiddle returns something like this:

["***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:

['***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.

 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() is often used for debugging, but is not entirely reliable . 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. Just keep in mind that because it can be unreliable, actual debugging should be used for more serious code and issues.

jsfiddle link

CodePen link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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