skip to Main Content

I’m not an experienced JavaScript programmer. This is my first project using JS.

I’m developing on Linux and am at the point for my first release. Running the project on a Windows machine I came across the following …

const os = require("os");
global.DBSysInfo;

console.log("DBSysInfo =n" + DBSysInfo);
var pnt1 = DBSysInfo.indexOf('DBActive = "yes"');
console.log("pnt1 = " + pnt1);
var pnt2 = DBSysInfo.indexOf(os.EOL + os.EOL, pnt1) + 1;
console.log("pnt1 = " + pnt1 + "; pnt2 = " + pnt2);
console.log("DBSysInfo.substring =n" + DBSysInfo.substring(pnt1, pnt2));

The above code works as expected on Linux:

DBSysInfo =
SysLocation = "C:UsersmlakeMELGenKey"  
  
DBActive = "yes"  
DBName = "SOT2-KILLE20240704"  
DBUserID = "2.0"  
DBStatus = "1"  
DBSecurity = "0"  
DBLocation = "DBs/SOT2-KILLE20240704"  


pnt1 = 42  
pnt1 = 42; pnt2 = 176  
DBSysInfo.substring =  
DBActive = "yes"  
DBName = "SOT2-KILLE20240704"  
DBUserID = "2.0"  
DBStatus = "1"  
DBSecurity = "0"  
DBLocation = "DBs/SOT2-KILLE20240704"  

On Windows, the output is:

DBSysInfo =  
SysLocation = "C:UsersmlakeMELGenKey"  
  
DBActive = "yes"  
DBName = "SOT2-KILLE20240704"  
DBUserID = "2.0"  
DBStatus = "1"  
DBSecurity = "0"  
DBLocation = "DBs/SOT2-KILLE20240704"  
  
  
pnt1 = 42  
pnt1 = 42; pnt2 = 0  
DBSysInfo.substring =  
SysLocation = "C:UsersmlakeMELGenKey"  

I don’t understand why the output would be different on Windows. Why would pnt2 be 0? The only thing I can think is the second "indexOf" statement is treating "DBSysInfo" as an array. If so, why? Is global treated differently on Windows than it is on Linux?

I tried using both Edge and Firefox on Windows with the same results, but the code is in node and I wouldn’t think the browser used would make any difference in this instance.

3

Answers


  1. Chosen as BEST ANSWER

    The answers and comments made me realize what was going on. (Kind of a "duh" moment for me!) The contents of "DBSysInfo" comes from reading a file that was created on Linux and just copied over to the Windows machine. The reverse could also happen as the project is used. Thanks, everyone, for your answers and comments.


  2. Is string.indexOf() treated the same on Linux vs Windows?

    Yes.

    The only thing I can think is the second "indexOf" statement is treating "DBSysInfo" as an array.

    That’s possible (not “treating”, but that DBSysInfo is somehow actually an array), but more likely is that the contents of the string aren’t what you expect, and that indexOf is returning −1 because the search pattern actually doesn’t exist in the string. In any case, you can confirm with either a debugger or print debugging:

    console.log("%O", DBSysInfo);
    

    The %O format specifier corresponds to inspect-type output that will quote strings, bracket arrays, and display non-printing characters as escapes.

    Even on Windows, it’s common to see Unix line endings, so looking strictly for os.EOL is prone to error. A regular expression that allows flexible line endings is a decent alternative to plug in:

    let pnt2 = DBSysInfo.indexOf(/(r?n){2}/m, pnt1);
    

    Or, if you’re not writing this string back, consider normalizing it as it arrives with .replace(/rn/g, "n"). Then you can search for "nn" directly and not have to also potentially worry about how many characters a newline is.

    Login or Signup to reply.
  3. The issue is not related to the js method. you are getting different output because there is a difference between newline characters on Linux and Windows operating systems.

    This is how you can solve it

    console.log("DBSysInfo =n" + DBSysInfo);
    
    // This line replaces all occurrences of rn with n, ensuring that the string has consistent newlines regardless of the operating system
    const normalizedDBSysInfo = DBSysInfo.replace(/rn/g, 'n');
    
    var pnt1 = normalizedDBSysInfo.indexOf('DBActive = "yes"');
    console.log("pnt1 = " + pnt1);
    
    //  To Find the next double newline sequence
    var pnt2 = normalizedDBSysInfo.indexOf('nn', pnt1) + 1;
    // For debugging only 
    console.log("pnt1 = " + pnt1 + "; pnt2 = " + pnt2);
    
    console.log("DBSysInfo.substring =n" + normalizedDBSysInfo.substring(pnt1, pnt2));
    

    Please let me know if this does not work properly. I have verified it, and it is working for me, but exceptions are always possible.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search