I have the code below.
{
var custname= "#1";
var file = "c:/temp/"+ custname + ".txt";
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.CreateTextFile(file, true, false);
fh.Write(text);
fh.Close();
}
The #1 is coming from a database and it's a customer's name. The above code works until I came across a customer name with a forward slash in it, e.g. "Company A/S".
How do I make this work for customers with the forward slash?
Replace any slashes in the customer name with another character (e.g. an underscore).
var custname= "#1";
custname = custname.replace(/\//g, '_');
...
Note the use of the g modifier on the regular expression to ensure that every slash character is replaced.
This is also why a regexp is used instead of just '/' - the substring version of String.replace won't handle repeated occurences.
You can replace (or delete) any characters not allowed by the filesystem. For example:
custname = custname.replace(/[/|&$]/g, '');
You're creating the file from the customers name as well, so when they have a '/' in their name, the file directory could look something like:
var custname= "/isuck";
var file = "c:/temp/"+ custname + ".txt";
File would equal:
c:/temp//isuck.txt
Which wouldn't be a valid file. The SMART move would be to not allow any '/' marks in a customer name, which really doesn't make sense away. Or if they HAVE to put it in that way, remove the '/' before storing it, or replace it with another character.
Store an id, eg a number, for each customer in the database and get the database to return this. Use the number for the filename. If you then need the name do another query.
If it's Windows you can't have a file name with "/" in it so personally I'd just replace all "/" with something else.
var file = "c:/temp/"+ custname.replace(/\//g, "-") + ".txt";
Related
I have a string made up of a page title and a page URL, separated by " - ". I would like to return a string which contains everything exept the parameters in the URL.
Input
This is a page title with a - and a ? - http://subdomain.example.com/subfolder/numbers134/?utm_medium=email&utm_source=a_source&utm_campaign=a_campaign_name
Desired Output
This is a page title with a - and a ? - http://subdomain.example.com/subfolder/numbers134/
What I have tried
I have tried .split() but because it is possible for the page title to have a "?" in it, this doesn't work.
x.replace(x.match(/http:[a-z/.0-9-]*(?.+)$/),"") where x is the string. This feels like it should have worked. I'm not sure if it is possible to use a capture group like this?
So far I have a regex to match the part I want removed: http:[a-z/.0-9-]*(?.+)$
I'm not sure how to turn that around and return the string minus that part.
Points
Page tile and URL separated by " - "
Page title may contain "-" or "?"
URL may contain parameters
The solution should work for any combination of 1. or 2. being present or not for any page title and URL.
The URL will always be http (Don't ask)
If you can control what the separator is then do that. make it less predictable and unique.
that way you can split by the new split character(s) and then pass the rest of string to the URL constructor
let SEPARATOR = '^^^';
let input = 'This is a page title with a - and a ? ^^^ http://subdomain.example.com/subfolder/numbers134/?utm_medium=email&utm_source=a_source&utm_campaign=a_campaign_name'
let url = new URL(input.substring(input.indexOf(SEPARATOR) + SEPARATOR.length));
// or
let url = new URL(input.split(SEPARATOR)[1])
console.log(url.origin + url.pathname)
// logs http://subdomain.example.com/subfolder/numbers134/
this way you also get for free the url validation logic
Could you not achieve it using lastIndexOf and substring like this?
var url = 'This is a page title with a - and a ? - http://subdomain.example.com/subfolder/numbers134/?utm_medium=email&utm_source=a_source&utm_campaign=a_campaign_name';
var output = url.substring(0, url.lastIndexOf('?'));
console.log(output);
I wonder if it's possible to get this part of a string.
Here is my string:
var string = "www.somesite.com/o/images%2Fc834vePyJ3SFVk2iO4rU0ke1cSa2%2F12391381_10205760647243398_2385261683139818614_n.jpg?alt=media&token=7a692a38-6982-474f-bea5-459c987ae575";
Now I want to be able to grab just this part of the string, the file name:
12391381_10205760647243398_2385261683139818614_n.jpg
I tried:
var result = /[^/]*$/.exec(""+url+"")[0];
, but it will return
user%2Fc834vePyJ3SFVk2iO4rU0ke1cSa2%2F12391381_10205760647243398_2385261683139818614_n.jpg?alt=media&token=4c92c4d7-8979-4478-a63d-ea190bec87cf
My Regex is wrong.
Another this is, the file extension can be .png or jpg so it's not fixed to jpg.
You could use a regex to isolate the part you want :
This works :
var string = "www.somesite.com/o/images%2Fc834vePyJ3SFVk2iO4rU0ke1cSa2%2F12391381_10205760647243398_2385261683139818614_n.jpg?alt=media&token=7a692a38-6982-474f-bea5-459c987ae575";
console.log((string.match(/[A-Za-z0-9_]+.(jpg|png|bmp)/))[0].substring(2));
Note that may have to be adapted depending on how much the URL string changes:
var string = "www.somesite.com/o/images%2Fc834vePyJ3SFVk2iO4rU0ke1cSa2%2F12391381_10205760647243398_2385261683139818614_n.jpg?alt=media&token=7a692a38-6982-474f-bea5-459c987ae575";
var out = string.split('?')[0].split('%2F')[2];
console.log(out); // "12391381_10205760647243398_2385261683139818614_n.jpg"
Assuming, you always have an url, first I would decode the encoded / (%2F) characters via:
var string = "www.somesite.com/o/images%2Fc834vePyJ3SFVk2iO4rU0ke1cSa2%2F12391381_10205760647243398_2385261683139818614_n.jpg?alt=media&token=7a692a38-6982-474f-bea5-459c987ae575";
var decodedUrl = decodeURIComponent(string);
and then use a regex:
decodedUrl.match(/[^/]*(?=[?])/)
Mind, that this regex assumes parameters (the part starting with ?...) are present, so if that's not the case, you might have to alter it to your needs.
If the filename always has a .jpg extension:
var url = decodeURIComponent(string);
var filename = url.substring(url.lastIndexOf("/")+1, url.lastIndexOf(".jpg"))
If not:
url = url.substring(url.lastIndexOf("/")+1)
filename = url.substring(0,url.indexOf("?"))
Looking at the string, it appears that the file name is between the second occurrence of "%2F" and the first occurrence of "?" in the string.
The first step is to get rid of the part of the string before the second "%2F". This can be done by splitting the string at every "%2F" and taking the third element in the resulting array.
var intermediate = string.split("%2F")[2]
Then, we need to get rid of everything after the "?":
var file_name = intermediate.split("?")[0]
This should give you the file name from the URL
I want to make sure that the URL I get from window.location does not already contain a specific fragment identifier already. If it does, I must remove it. So I must search the URL, and find the string that starts with mp- and continues until the end URL or the next # (Just in case the URL contains more than one fragment identifier).
Examples of inputs and outputs:
www.site.com/#mp-1 --> www.site.com/
www.site.com#mp-1 --> www.site.com
www.site.com/#mp-1#pic --> www.site.com/#pic
My code:
(that obviously does not work correctly)
var url = window.location;
if(url.toLowerCase().indexOf("#mp-") >= 0){
var imgString = url.substring(url.indexOf('#mp-') + 4,url.indexOf('#'));
console.log(imgString);
}
Any idea how to do it?
Something like this? This uses a regular expression to filter the unwanted string.
var inputs = [
"www.site.com/#mp-1",
"www.site.com#mp-1",
"www.site.com/#mp-1#pic"
];
inputs = inputs.map(function(input) {
return input.replace(/#mp-1?/, '');
});
console.log(inputs);
Output:
["www.site.com/", "www.site.com", "www.site.com/#pic"]
jsfiddle: https://jsfiddle.net/tghuye75/
The regex I used /#mp-1?/ removes any strings like #mp- or #mp-1. For a string of unknown length until the next hashtag, you can use /#mp-[^#]* which removes #mp-, #mp-1, and #mp-somelongstring.
Use regular expressions:
var url = window.location;
var imgString = url.replace(/(#mp-[^#\s]+)/, "");
It removes from URL hash anything from mp- to the char before #.
Regex101 demo
You can use .replace to replace a regular expression matching ("#mp-" followed by 0 or more non-# characters) with the empty string. If it's possible there are multiple segments you want to remove, just add a g flag to the regex.
url = url.replace(/#mp-[^#]*/, '');
The window.location has the hash property so... window.location.hash
The most primitive way is to declare
var char_start, char_end
and find two "#" or one and the 2nd will be end of input.
with that... you can do what you want, the change of window.location.hash will normally affect the browser adress.
Good luck!
Good Morning,
I'm using a function I got from another post here:
How to get the file name from a full path using JavaScript?
To return the current filename of a webpage
var url=window.location.pathname;
var filename = url.replace(/^.*[\\\/]/, '');
alert(filename);
and I was curious as to whether it is possible to strip the .html from the end while still using this syntax.
I think the easiest way will be
var filename = url.replace(/^.*[\\\/]/, '').replace(".html", "");
Either specify the extensions you want to manage, to be more restrictive, using:
.replace(/^.*[\/](.*)[.](html|jsp|php)/, '$1');
Either be more generic, using:
.replace(/^.*[\/](.*)[.][a-zA-Z0-9]{2,5}/, '$1');
The second one will allow extensions from 2 chars (e.g. .do) to 5 chars (e.g. .xhtml), which could also contain numbers (e.g. .php3).
You can use a capture clause in replace:
var url=window.location.pathname;
alert(url)
var filename = url.replace(/^.*[\\\/](.*).html$/, '$1');
alert(filename);
how to use href.replace in extjs
This is my sample:
'iconCls': 'icon_' + href.replace(/[^.]+\./, '')
href= http://localhost:1649/SFM/Default.aspx#/SFM/config/release_history.png
Now i want to get text "release_history.png", How i get it.
Thanks
If you just want the filename, it's probably easier to do:
var href = "http://localhost:1649/SFM/Default.aspx#/SFM/config/release_history.png";
var iconCls = 'icon_' + href.split('/').pop();
Update
To get the filename without the extension, you can do something similar:
var filename = "release_history.png";
var without_ext = filename.split('.');
// Get rid of the extension
without_ext.pop()
// Join the filename back together, in case
// there were any other periods in the filename
// and to get a string
without_ext = without_ext.join('.')
some regex solutions (regex including / delimiter)
as in your example code match the start of the url that can be dropped
href.replace(/^.*\//, '')
or use a regex to get the last part of the url that you want to keep
/(?<=\/)[^.\/]+\.[^.]+$/
update
or get the icon name without .png (this is using lookbehind and lookahead feature of regex)
(?<=\/)[^.\/]+(?=\.png)
Not all flavors of regex support all lookaround reatures and I think Javascript only supports lookahead. so probably your solution is this:
[^.\/]+(?=\.png)
code examples here:
http://www.myregextester.com/?r=6acb5d23
http://www.myregextester.com/?r=b0a88a0a