This question already has answers here:
Need a basename function in Javascript
(19 answers)
Closed 8 years ago.
I have a string like C:\Users\shail.jet\Desktop\cca-lan_test_cases_path.txt.
I want to get only file name that is in this case is cca-lan_test_cases_path.txt.I have tried it with javascript split function but it did'nt work. Any help will be appreciated.
"C:\\Users\\shail.jet\\Desktop\\cca-lan_test_cases_path.txt".split("\\").pop();
Make sure you add escape backslashes in the file path string otherwise javascript will ignore it.
That will split by backslash and then use pop to get the last element in the array which will be the file name.
var filename = fullPath.replace(/^.*[\\\/]/, '')
This will handle both \ OR / in paths
var filename = fullPath.replace(/^.*(\\|\/|\:)/, '');
should also protect from empty string
Related
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 1 year ago.
I want to use regex to allow/reject files during fileupload with a specific file type.
This is the working regex /(\.|\/)(xlsx)$/i expected output that I want to achieve.
What I'm trying is instead of hardcode the filetype, I want to pass a variable that holds filetype into the regex.
var fileType = 'xlsx';
var regexText = new RegExp("/(\.|\/)(" + fileType + ")$/i");
but the current output of regexText is /\/(.|\/)(xlsx)$\/i/ this is not same as expected output. How do I can get same output as mentioned above by using a variable without hardcode? Is there solution without using the RegExp that can give the same expected output?
try forming string for regex line before calling new RegExp
This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 2 years ago.
Please note that this is not a json object :) My string is:
{"message":"***error in SAP module:-1***","status":400}
This is not a json object, this is a pure string. I cannot turn it into a json object due to technical limitations.
So, I want to take only the bold string (all the value of "message").
I thought about lastIndexOf, and pick the string between ":" and ","
But I got messed up with the escape characters for the quotes.
How can I achieve it with lastIndexOf? Or in another better way?
If you can't use JSON.parse, I would use a regex to read it. You know you want the string directly following "message":", so I would look for that, then grab everything from there to the next occurrence of "
This question already has answers here:
Regex string between underscore
(2 answers)
Closed 2 years ago.
In the above string, I want to retrieve 123 only which is between the _ (underscore) character. Can someone please help me using JAVA SCRIPT?
You should split your string by _ underscore delimiter. You will have an array which contains the parts of the "original" string. You can get the parts of the "original" string by using the indexes. You can see a snippet below.
Code part:
var str = 'ID_123_george'.split('_'); //SPLIT INTO ARRAY
str[1]; //1st index will give you 123
This question already has answers here:
Replace forward slash "/ " character in JavaScript string?
(9 answers)
Why this javascript regex doesn't work?
(1 answer)
Closed 4 years ago.
I have a string field 01/01/1986 and I am using replace method to replace all occurrence of / with -
var test= '01/01/1986';
test.replace('//g','-')
but it does't give desire result. Any pointer would be helpful.
You just have a couple issues: don't put the regex in quotes. That turns it into a string instead of a regex and looks for that literal string. Then use \/ to escape the /:
var test= '01/01/1986';
console.log(test.replace(/\//g,'-'))
A quick way is to use split and join.
var test= '01/01/1986';
var result = test.split('/').join('-');
console.log(result);
Note too that you need to save the result. The original string itself will never be modified.
This question already has answers here:
How to remove text from a string?
(16 answers)
Closed 7 years ago.
In JavaScript, I need to find a substring between &J= and Key using regex and remove it from my url which could contain several substrings.
Here is my url:
SID=18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003
Thanks
You can learn Regular Expression here and code for your problem is:
Here you go:
SID="18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003";
var re = /&J=(.*?)&Key/;
SID = SID.replace(re, '');
alert(SID);
See Fiddle