Re: CSV and regex s.split(",") and empty fields

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I can reproduce the issue that the String.split() using regular expression as parameter in JavaScript has different behaviors in IE and Mozilla/Firefox. I have located the record in our internal system and it is a known issue. Currently, the issue is not fixed yet and the current scripting engine will be replaced by a new one in IE 9.

You also can post the feedback on the Connect Website (https://connect.microsoft.com/IE/Feedback). Our developer will evaluate them seriously and take them into consideration when designing future release of the product.

Hello Thomas,

Thanks for confirmation of this problem. That was the key point of my post. Please note that I'm using WSH on Vista, I'm not using IE. Will the IE9 fix also fix the issue in WSH?

Thanks.

X-Tomcat-NG: microsoft.public.scripting.jscript

Hello Thomas,

Thnak you for the help with this.

Are you able to see all the replies in this thread? There are around 12 posts in total to date.

The key point is that there appears to be a BUG in the Microsoft regex implementation of the JScript String.split() method.

// cscript split.js
// Split string
var t = 'one,two,,four,,,seven,eight';
var a = t.split(",");
var n = a.length;
WScript.Echo(n); // prints 8
var a = t.split(/,/);
var n = a.length;
WScript.Echo(n); // prints 5

Firefox and Chrome return 8 in both cases, which I believe is correct. Do you agree this is a BUG in Microsoft JScript?

In relation to splitting delimited files that have quoted fields,
delimiters
within quotes, empty fields, non-quoted fields, and leading/trailling delimiters, I believe the code examples I posted on the 30th Aug 09 are
the
best solution I've seen so far.

Thanks.

--
Gerry Hickman
London (UK)

"Thomas Sun [MSFT]" <v-thsun@xxxxxxxxxxxxxxxxxxxx> wrote in message news:RLMd82hKKHA.4732@xxxxxxxxxxxxxxxxxxxxxxxxx
Hi Gerry,

If the string is using comma to separate name and using quotation mark to
include name, such as " "username", "lastname, firstname","nextthing" ", we
can use ' " ' to split it and then remove ' , '. For example, we can use
follow code to do this:
==============================
<script type="text/javascript">

var result = '', tempStr = '';
var element;
var t = '"username", " lastname, firstname","nextthing"';
for (var j = 0; j < t.length; j++) {
if (t.charAt(j) != ' ')
tempStr += t.charAt(j);
}

var n = tempStr.split('"');
for (var i = 0; i < n.length; i++) {
element = n[i];
if (element != undefined && element != ',' && element !=
'') {
result += element + ' ';
}
}

alert(result);
</script>
=============================

What format of the string do you want? I understand that it will give
customer well experience if we can handle all strings. But I think it is
hard to find a solution to parse the string with many different formats
correctly. It would be better that we can specify the string format and
then coding basing on it.

Besides, we also can consider using another delimiter, such as '; ', to
separate name. By doing so, it makes coding easier.

I look forward to receiving hearing from you.


--
Best Regards,
Thomas Sun

Microsoft Online Partner Support

==================================================
Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as
opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Dr J R Stockton wrote:

NOW there is.

t = 'one,two,,four,,,seven,eight';

String.prototype.Split = function() {
var A = [], S= "", J
for (J=0 ; J<this.length ; J++) {
if (this.charAt(J)==",") { A.push(S) ; S = "" } else S +=
this.charAt(J) }
if (S>"") A.push(S)
return A }

X = t.Split().join("+") // one+two++four+++seven+eight
Thanks. This works, but perhaps would fall over with more complex
delimiter arrangements such as

"username", "lastname, firstname","nextthing"

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
I just had a look at the FAQ, is there something specific I need to look
at?
--
Gerry Hickman (London UK)





--
Gerry Hickman (London UK)
.



Relevant Pages

  • A general solution
    ... Split on an array of delimiters; parse a file in one call. ... Hi Gerry,If the string is using comma to separate name and using quotation ... var element; ... Gerry Hickman wrote: ...
    (microsoft.public.scripting.jscript)
  • Re: CSV and regex s.split(",") and empty fields
    ... var n = a.length; ... In relation to splitting delimited files that have quoted fields, delimiters within quotes, empty fields, non-quoted fields, and leading/trailling delimiters, I believe the code examples I posted on the 30th Aug 09 are the best solution I've seen so far. ... If the string is using comma to separate name and using quotation mark to ... Microsoft Online Partner Support ...
    (microsoft.public.scripting.jscript)
  • RE: Extracting Terms and Exponents of a Text Poly Formula ??
    ... ' Count the number of words in a text string ... ' delimiters text string all characters that may terminate or separate ... --Term 1: power of var 1 ...
    (microsoft.public.excel.worksheet.functions)
  • RE: Extracting Terms and Exponents of a Text Poly Formula ??
    ... ' Count the number of words in a text string ... ' delimiters text string all characters that may terminate or separate ... --Term 1: power of var 1 ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Calculate the string statement
    ... verkn:(vkn:char; ... funkt:(fkt:string; ... var fkt,dfkt: p; ... var fehler: boolean; ...
    (comp.lang.pascal.borland)