I have a string like
/abc/def/hij/lmn.o // just a raw string for example dont know what would be the content
I want only /abc/def/hij part of string how do I do that.
I tried using .split() but did not get any solution.
If you want to remove the particular string /lmn.o, you can use replace function, like this
console.log(data.replace("/lmn.o", ""));
# /abc/def/hij
If you want to remove the last part after the /, you can do this
console.log("/" + data.split("/").slice(1, -1).join("/"));
# /abc/def/hij
you can do
var str = "/abc/def/hij/lmn.o";
var dirname = str.replace(/\/[^/]+$/, "");
Alternatively:
var dirname = str.split("/").slice(0, -1).join("/");
See the benchmarks
Using javascript
var x = '/abc/def/hij/lmn.o';
var y = x.substring(0,x.lastIndexOf("/"));
console.log(y);
var s= "/abc/def/hij/lmn.o"
var arr= s.split("/");
after this, use
arr.pop();
to remove the last content of the array which would be lmn.o, after which you can use
var new_s= arr.join("/");
to get /abc/def/hij
Related
it didn't work on stackoverflow snippet but if you try on your local you will see it's going to work.
js give me a this result
cdn=//cdn.files.com/web
but I dont want to this line
cdn=
js must give me after from cdn= I mean result must be like this
//cdn.files.com/web
my all js is below so how to do that ?
var scripts = document.getElementsByTagName('script');
var index = scripts.length - 1;
var myScript = scripts[index];
// myScript now contains our script object
var queryString = myScript.src.replace(/^[^\=]+\??/,'');
alert(decodeURIComponent(queryString));
<script src="//domain.com/web/Assets/js/main.js?cdn=%2f%2fcdn.files.com%2fweb"></script>
<p></p>
var s = "cdn=//cdn.files.com/web";
s2 = s.substring(s.indexOf("cdn=")+4,s.length);
alert(s2);
this will substring from the index next to equals character to the end of yours string :)
fixed and tested
Split the string by '?cdn=' and get the part after that(second element in the result array, at index 1).
var queryString = myScript.src.split('?cdn=')[1];
FYI : If there is only one URL param then you can simply use = or cdn= for splitting.
You can use split:
yourvariable.split('=');
So whatever your arguments into GET, the odd param will get your desired result.
PS: Prefer just the = char, because you can work with whatever param you want into link gived by src.
why can't you use ^.+?cdn= in your regex?
this will give you strings after the cdn=
var myScript = '//domain.com/web/Assets/js/main.js?cdn=%2f%2fcdn.files.com%2fweb';
var queryString = myScript.replace(/^.+?cdn=/, '');
console.log(decodeURIComponent(queryString));
Solution with regex
var src = 'cdn=//cdn.files.com/web';
var url = src.replace(/^([^\=]+=)(.*)$/, '$2');
console.log(decodeURIComponent(url));
DEMO with explanation
I have the following string:
var fileName = $(this).val();
this will give me a result:
C:\fakepath\audio_recording_47.wav
what I want is to obtain : audio_recording_47.wav
so, I need to trim it but I don't know how using javascript
please help
filename.split('\\').reverse()[0];
This will split the path by slashes, to obtain each part. Then to keep it simple, i reverse the array, so the last part that you need is now the first; and get the first part.
Or, even more simply: filename.split('\\').pop(), which will get the last item from the array.
You could write a little function to return the base name of the path:
function basename(fn) {
var x = fn.lastIndexOf("\\");
if (x >= 0) return fn.substr(x + 1);
return fn;
}
var filename = basename($(this).val());
You can do like this:
var fileName = $(this).val();
var path = fileName.split('\\');
var lastValue = path[path.length-1];
console.log(lastValue);//audio_recording_47.wav
Or, the shorter way you can do like this:
var fileName = $(this).val();
var path = fileName.split('\\').slice(-1);//audio_recording_47.wav
This should do it:
var trimmedFileName = fileName.match(/[^\\]*$/);
It matches everything that isn't a \ until the end of the string.
You could use a regular expression, like this:
var fileName = this.value.replace(/(?:[^\\\/]*[\\\/])*/, '');
Also, there is no need to use that snippet of jQuery, as this.value is both faster and simpler.
I want to remove the last parameter in the href of a specific tag using jquery
for example replace href="app/controller/action/05/04/2014"
by href="app/controller/action/05/04"
Try using the String.lastIndexOf() and String.substring() in this context to achieve what you want,
var xText ="app/controller/action/05/04/2014";
xText = xText.substring(0,xText.lastIndexOf('/'));
DEMO
if you know which value you need to change ,than you can use replace:
var str = "app/controller/action/05/04/2014";
var res = str.replace("2014","04");
or else you can use array and change / update last value in array:
var str = "app/controller/action/05/04/2014";
var res = str.split("/");
We can use a regular expression replace which will be the fastest, compared to substring/slice.
var hreftxt ="app/controller/action/05/04/2014";
hreftxt = hreftxt.replace(/\/[^\/]*$/,"");
Javascript:
var string = '(37.961523, -79.40918)';
//remove brackets: replace or regex? + remove whitespaces
array = string.split(',');
var split_1 = array[0];
var split_2 = array[1];
Output:
var split_1 = '37.961523';
var split_2 = '-79.40918';
Should I just use string.replace('(', '').replace(')', '').replace(/\s/g, ''); or RegEx?
Use
string.slice(1, -1).split(", ");
You can use a regex to extract both numbers at once.
var string = '(37.961523, -79.40918)';
var matches = string.match(/-?\d*\.\d*/g);
You would probably like to use regular expressions in a case like this:
str.match(/-?\d+(\.\d+)?/g); // [ '37.961523', '-79.40918' ]
EDIT Fixed to address issue pointed out in comment below
Here is another approach:
If the () were [] you would have valid JSON. So what you could do is either change the code that is generating the coordinates to produce [] instead of (), or replace them with:
str = str.replace('(', '[').replace(')', ']')
Then you can use JSON.parse (also available as external library) to create an array containing these coordinates, already parsed as numbers:
var coordinates = JSON.parse(str);
Let’s say I have test_23 and I want to remove test_.
How do I do that?
The prefix before _ can change.
My favourite way of doing this is "splitting and popping":
var str = "test_23";
alert(str.split("_").pop());
// -> 23
var str2 = "adifferenttest_153";
alert(str2.split("_").pop());
// -> 153
split() splits a string into an array of strings using a specified separator string.
pop() removes the last element from an array and returns that element.
If you want to remove part of string
let str = "try_me";
str.replace("try_", "");
// me
If you want to replace part of string
let str = "try_me";
str.replace("try_", "test_");
// test_me
Assuming your string always starts with 'test_':
var str = 'test_23';
alert(str.substring('test_'.length));
Easiest way I think is:
var s = yourString.replace(/.*_/g,"_");
string = "test_1234";
alert(string.substring(string.indexOf('_')+1));
It even works if the string has no underscore. Try it at http://jsbin.com/
let text = 'test_23';
console.log(text.substring(text.indexOf('_') + 1));
You can use the slice() string method to remove the begining and end of a string
const str = 'outMeNo';
const withoutFirstAndLast = str.slice(3, -2);
console.log(withoutFirstAndLast);// output--> 'Me'