-2

I have a scenario in which I need to trim the cells in csv, but I need to retain the spaces in between the text and should not trim the text in quotes but trim the spaces around quotes like other cells.

example:

 -->     first one, secondone ,  " third one "  , fourth one  

Should be

 -->first one,secondone,"third one",fourth one

I have followed this link which partially answers my question but it removes spaces in between words too. I have done some work on this item but not successful.Please assist in getting this resolved. Also when I tried to apply the answer posted in the link ( which is aimed to c#) I got warning stating not all browsers will support "negative lookbehind". Is there any alternative regex to achieve this ?

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
ganesh310
  • 35
  • 6
  • 1
    This seems like it might be an [XY Problem](http://xyproblem.info/). If you're trying to parse CSV-formatted data, using a dedicated CSV parser will take care of lots of edge cases you would forget about otherwise. You can strip whitespace after the initial parsing. – John Ellmore Aug 21 '18 at 14:42
  • I am not supposed to use any third party parser in my project. Also strin.trim () wont work . I wrote my own logic but will not work in case of multiple , in quotes like " a, b, c " – ganesh310 Aug 21 '18 at 14:47
  • @ganesh310 Is this what you want: http://jsfiddle.net/ejzhgcLa/? – Unmitigated Aug 21 '18 at 17:31
  • Following on from @JohnEllmore's comment about this being an XY problem, and your comments on my answer below, I'm wondering if the real problem you're trying to solve is that you want to split the String on commas, but not when those commas are contained within `"`? Maybe you're trying to resolve that by first removing the leading and trailing spaces? It would be better if you can edit your question to explain the full requirement. – DaveyDaveDave Aug 22 '18 at 07:34

2 Answers2

1

You can try this regex:

var a='     first one, secondone ,  " third one "  , fourth one  ';
console.log(a.replace(/\b\s*\B|\B\s*\b|\B\s*\B/g,''));//logs 'first one,secondone,"third one",fourth one'
  • \b Word boundary
  • \B Not a word boundary

Spaces between any combination of above is replaced(except the space between \b and \b).

TheMaster
  • 45,448
  • 6
  • 62
  • 85
0

Assuming that your aim is either to output a CSV-formatted string without these spaces, or to read values from a CSV file that may or may not have these spaces, I think the best solution will be one of the following:

If you're trying to output CSV:

Presumably you have some collection of Strings where each String may or may not have leading and/or trailing spaces. In this case, the best option will be to iterate over the collection of Strings and call String.trim() on each one - the trim() method removes leading and trailing spaces.

Once you have that working, you can probably be more efficient by including that call to trim() in whatever code is building the String.

If you're reading a CSV file:

You presumably have a String representing a single line from the file, and you're splitting that on the , characters to create a collection of Strings. As above, you can then iterate over that collection, calling trim() on each String.

DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77