1

I am trying to remove commas in a string unless they appear inside quotes.

var mystring = "this, is, a, test, example, \"i, dont know\", jumps" ;
var newchar = '';

mystring = mystring.split(',').join(newchar);// working correctly


document.write(mystring);

Output I have is

this is a test example "i dont know" jumps

Expected output

this is a test example "i, dont know" jumps

A couple of questions. How can I find the index of string so that inside the quotation it will include comma but outside of quotation " it will not include comma ,. I know I have to use indexOf and substring but I don't know how to format it? (No regex please as I'm new to JavaScript and I'm just focusing on the basics.)

Seth
  • 37
  • 5

3 Answers3

3

Loop through the string, remembering whether or not you are inside a set of quotation marks, and building a new string to which the commas inside quotes are not added:

var inQuotes = false;                      // Are we inside quotes?
var result = '';                           // New string we will build.

for (var i = 0; i < str.length; i++) {     // Loop through string.
  var chr = str[i];                        // Extract character.
  var isComma = chr === ',';               // Is this a comma?
  var isQuote = chr === '"';               // Is this a quote?

  if (inQuotes || !isComma) {              // Include this character?
    if (isQuote) inQuotes = !inQuotes;     // If quote, reverse quote status.
    result += chr;                         // Add character to result.
  }
}

This solution has the advantage compared to the accepted one that it will work properly even if the input has multiple quotes strings inside it.

  • Nice, and fast. :) Just started to work on less elegant solution, so I give up. :) – sinisake Jan 25 '17 at 03:05
  • @torazaburo i just cannot understand this code. I love how simple this can be. I just did not understand this code. `if (isQuote) inQuotes = !inQuotes; ` . If I am not wrong isnt if statement starts with ` if (){}` – Seth Jan 25 '17 at 04:33
  • If you want to put `{}` around `inQuotes = !inQuotes;`, then go ahead. It's not necessary though. What follows an `if` statement can be **either** a single statement, as I've written it, **or** a block in `{}`. –  Jan 25 '17 at 04:39
  • @torazaburo when i put curly bracket its giving me error thats why i was confuse – Seth Jan 25 '17 at 11:32
1

This will work, but it's not ideal for all cases. Example: It will not work for a string with more than 2 quotation marks.

var mystring = "this, is, a, test, example, \"i, dont know\", jumps" ;
var newchar = '';
var firstIndex = mystring.indexOf("\"");
var lastIndex = mystring.lastIndexOf("\"");
var substring1 = mystring.substring(0,firstIndex).split(',').join(newchar);
var substring2 = mystring.substring(lastIndex).split(',').join(newchar);
mystring = substring1 + mystring.substring(firstIndex, lastIndex) + substring2;

document.write(mystring);
Riya
  • 180
  • 2
  • 15
  • can we find the indexof \" using if ? I appreciate your help towards me – Seth Jan 25 '17 at 01:41
  • 1
    Could you please elaborate what you mean? Right now the code finds the index of ". – Riya Jan 25 '17 at 01:43
  • can we find indexOf using if statment ? just wondering. I gave your answer as a best answer. Thank you – Seth Jan 25 '17 at 01:56
  • Thank you! IF you want to use if statement, there should be some value to which you want to compare with. I'm not sure how would you simply use if statement to find any index value which you don't know already. You may need to convert string into an array and then find if indexOf array has a quotation mark. – Riya Jan 25 '17 at 02:04
0

Some day you need to start using regexp, than regexr.com is your friend. The regexp solution is simple:

var mystring = "this, is, a, test, example, \"i, dont know\", jumps" ;
var newchar = '_';

mystring = mystring.match(/(".*?"|[^",\s]+)(?=\s*,|\s*$)/g).join(newchar);// working correctly

document.write(mystring);
eapo
  • 1,053
  • 1
  • 19
  • 40