RegExp and backslash

I'm trying to replace in a string any backslash (\) with a normal slash(/).
The string .replace method requires a RegExp as parameter. My current code is the following:

var regexBackslash = new RegExp("\\\\");while(sPath.search(regexBackslash)>=0){   sPath=sPath.replace(regexBackslash,"/");}

This works but the RegExp("\\\\") part acts odd in the script IDE as any comment afterwards (with //) is colored like its code.
Do I have to escape the the backslash differently?

Comments

  • OmnifluxOmniflux Posts: 359

    No, that's just an IDE issue we live with. The IDE is not escaping properly and is acting as if you are escaping the closing quote. Despite this, the script engine handles it correctly.

    Someone should report this to get it fixed...

     

    This will not solve the issue, but if you use regex literal syntax, you don't have to double escape

    var regexBackslash = new RegExp(/\\/);

    And if you use the global flag, you can get rid of the loop

    var regexBackslash = new RegExp(/\\/g);

    But wait! Literal notation compiles a regular expression when evaluated, and the RegExp constructor compiles (or copies) it at runtime! Simplify!

    var regexBackslash = /\\/g;

    At this point, the commonly used pattern is

    sPath=sPath.replace(/\\/g,'/');

     

  • mikekmikek Posts: 192

    Thank you, very helpful. Especially happy for the way to get rid of the loop.
    I have also sent a ticket for the bug so they can fix it with a future release.

  • Why are you using a regexp for something as simple as searching for a backslash?

  • OmnifluxOmniflux Posts: 359

    @TheMysteryIsThePoint

    How would you solve? Calling replace with a string only replaces the first instance, and split then join is quite a bit slower.

    I don't know the use case here, but if I was just doing this for a few files, I would probably use DzFileInfo and let it sort it out on its own.

  • mikekmikek Posts: 192
    edited August 2022

    TheMysteryIsThePoint said:

    Why are you using a regexp for something as simple as searching for a backslash?

    From what I have seen there is no specific function to replace all occurrences ​like some other languages have and the replace function expects a RegExp according to documentation:
    http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/object_index/string

    Post edited by mikek on
  • We are told that string is also a way to handle this https://262.ecma-international.org/5.1/#sec-15.5.4.11 , and that it also possible for newValue to be a function

  • Richard HaseltineRichard Haseltine Posts: 96,219
    edited August 2022

    We are told that the documents have been updated to reflect the above. replace has three overlaoded versions starting here http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/object_index/string#a_1ad0ff93407a4b5a330f918a18a6a0aff9

    Post edited by Richard Haseltine on
  • Omniflux said:

    @TheMysteryIsThePoint

    How would you solve? Calling replace with a string only replaces the first instance, and split then join is quite a bit slower.

    I don't know the use case here, but if I was just doing this for a few files, I would probably use DzFileInfo and let it sort it out on its own.

    @Omniflux

    Probably looping exactly like you describe, as I can't imagine it making a requirements-voiding difference. If I can tax the computer or the human trying to understand the code, I prefer to make things easier for the human, which most of the time is future-me. But you're right; the best way for most cases is probably just QFileInfo.

  • mikekmikek Posts: 192
    edited August 2022

    TheMysteryIsThePoint said:

    the best way for most cases is probably just QFileInfo.

    DzFileInfo would also work but the only way I know how is for each time when combining two paths (i don't know the formating of) adding "/" in between.
    So it would be like:
     

    var oPathInfo=new DzFileInfo(sPath1+"/"+sPath2);

    Adding afterwards the file name would be:

    var oFileInfoAbsolute=new DzFileInfo(oPathInfo.absoluteFilePath()+"/"+sFile);

    The better approach as I need it more often is imo to write a function which can combine two parts of a path and returns a valid new path. So something like "c:/test" +"test2" creates "c:/test/test2/" and + "file.duf" creates "c:/test/test2/file.duf". I'm also used to having such a function but haven't found one.

    Post edited by mikek on
  • TheMysteryIsThePointTheMysteryIsThePoint Posts: 2,882
    edited August 2022

    Edit: Sorry, I just realized this is the Scripting forum, not the SDK forum...

     

    Is DzFileInfo missing from anyone else's SDK install?

    Also found this:

    QString DzFileIO::getFilePath (    const QString &filename ) [static]

    Parameters:
            filename     File path to operate on.

    Returns:
        The path of the file - the filename from the beginning up to (but not including) the last path separator.

    Example:

            QString path1 = DzFileIO::getFilePath( "/user/filename.txt" );      // path1 = "/user"
            QString path2 = DzFileIO::getFilePath( "C:\\temp\\filename.JPG" );  // path2 = "C:\temp"

     

     

    Post edited by TheMysteryIsThePoint on
Sign In or Register to comment.