RegExp and backslash
mikek
Posts: 195
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
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
And if you use the global flag, you can get rid of the loop
But wait! Literal notation compiles a regular expression when evaluated, and the RegExp constructor compiles (or copies) it at runtime! Simplify!
At this point, the commonly used pattern is
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?
@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.
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
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
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
@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.
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:
Adding afterwards the file name would be:
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.
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"