I am trying to build a small utility in javascript that can fix user input errors. The user is supposed to enter a string in the following format:
example: object1:"key1 key2" object2:"key3 key4" object2:"key5" object2:"key6 key7" What I am trying to create is a logic that will find missing quotation marks (either opening or closing) on the keys, in case the user forgot to add it.
possible error case would be: object1:"key1 object2:"key2" which should be fixed with by adding a closing " to the key1 object.
I'm a coding beginner and I am not looking for a ready solution, but helpful suggestions how I can approach this. Thank you so much!
If you are only concerned about the missing quotation marks and that the input string strictly only has missing quotation marks and follows your example format, one approach is to split the input in every colon character.
After that, you can loop through the array starting at the second element and check if the beginning of every string has opening quotation marks and if the characters before the object names are a quotation mark and a space. If not, add a opening/closing quotation mark.
After adding necessary quotation marks, you can just join the string back.
Again, that is if there are no other possible cases/scenarios that are needed to be checked, otherwise, you need to create a string parser that satisfies all possible cases.