Hey so I am trying to get all the substrings that match a Regex in a string as an array.
Example string- "[file/path/name.exe]value"
I need to get [file/path/name.exe]
as the result.
Few more examples -
"[file/path/name.exe][file/path/name1.exe]val"
Result -["[file/path/name.exe]", "[file/path/name.exe]"]
"[file/path/name1.exe][value][file/path/name2.exe]some_value"
Result -["[file/path/name1.exe]", "[file/path/name2.exe]"]
"[file/path/name1.exe]http:/\[file/path/name2.exe]some_value"
Result -["[file/path/name1.exe]", "[file/path/name2.exe]"]
So basically I need to extract anything that's between brackets [] and is a file path that ends with ".exe"
This is the Regex I tried so far -
let reg = new RegExp(/\[\S+\.exe\]/gi)
This gives the following results -
And string.match()
function in java script to get the results as an array.
"[file/path/name.exe]value"
Result -["[file/path/name.exe]"]
. Works for this case.But for this case, it doesn't work
"[file/path/name.exe][file/path/name1.exe]val"
I am getting this -["[file/path/name.exe][file/path/name1.exe]"]
as one element.And same for this case too
"[file/path/name1.exe][value][file/path/name2.exe]some_value"
I am getting this -["[file/path/name1.exe][value][file/path/name2.exe]"]
as one string.
So I tried doing negative look ahead like there should be only one opening bracket "[". But that didn't workout.
Regex was - let reg = new RegExp(/\[\S+\.exe\]$(?!\[)/gi)
but it doesn't work for any case.
I also tried to limit the word ".exe" to one but also didn't work.
Regex was-let reg = new RegExp(/\[\S+\(\b\.exe\b){1}\]/gi)
As you can see I am not an expert in Regex. Any pointers would be appreciated. Thanks.
PS: Apologies for any formatting issues. Tried my best on a phone :-).