Can someone please help. I need to get the characters between two slashes e.g:
Car/Saloon/827365/1728374
I need to get the characters between the second and third slashes. i.e 827365
You can use the split() method of the String prototype, passing in the slash as the separator string:
const value = 'Car/Saloon/827365/1728374';
const parts = value.split('/');
// parts is now a string array, containing:
// [ "Car", "Saloon", "827365", "1728374" ]
// So you can get the requested part like this:
const interestingPart = parts[2];
It's also possible to achieve this as a one-liner:
const interestingPart = value.split('/')[2];
Documentation is here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
This will simply alert 1728374, as you want
alert("Car/Saloon/827365/1728374".split('/')[3]);
or, a bit longer, but also more readable:
var str = "Car/Saloon/827365/1728374";
var exploded = str.split('/');
/**
* str[0] is Car
* str[1] is Saloon
* str[2] is 827365
* str[3] is 1728374
*/
Try the_string.split('/') - it gives you an array containing the substrings between the slashes.
try this:
var str = 'Car/Saloon/827365/1728374';
var arr = str.split('/'); // returns array, iterate through it to get the required element
Use split to divide the string in four parts:
'Car/Saloon/827365/1728374'.split('/')[2]
Gives "827365"
You will have to use the .split() function like this:
("Car/Saloon/827365/1728374").split("/")[2];
"Car/Saloon/827365/1728374".split("/")[2]
"Car/Saloon/827365/1728374".split("/")[3]
How many you want you take it.. :)
var str = "Car/Saloon/827365/1728374";
var items = str.split("/");
for (var i = 0; i < items.length; i++)
{
alert(items[i]);
}
OR
var lastItem = items[items.length - 1]; // yeilds 1728374
OR
var lastItem1728374 = items[2];
Related
I have couple of strings like this:
Mar18L7
Oct13H0L7
I need to grab the string like:
Mar18
Oct13H0
Could any one please help on this using JavaScript? How can I split the string at the particular character?
Many Thanks in advance.
For var str = 'Mar18L7';
Try any of these:
str.substr(0, str.indexOf('L7'));
str.split('L7')[0]
str.slice(0, str.indexOf('L7'))
str.replace('L7', '')
Based on input that is given it I have created following function which can take n string in array and return the output in the format you have given. Check if this helps and if some use case is missed.
function generateStr(arr, splitStr) {
const processedStr = arr.map(value => value.split(splitStr)[0]);
return processedStr.join(" OR ");
}
console.log(generateStr(["Mar18L7", "Oct13H0L7"], "L7"));
You can use a regex like this
var data = ["Mar18L7", "Oct13H0L7"];
var regex = /^([a-zA-Z0-9]+)\L[a-zA-Z0-9]+$/;
var output = []
data.forEach(function(el){
var matches = el.match(regex);
output.push(matches[1]);
});
output variable will be equal to ['Mar18', 'Oct13H0'] and you can join all value usin the .join method on output array
var chain = output.join(" OR ");
// chain will be equal to "Mar18 OR Oct13H0"
I have a string like "home/back/step" new string must be like "home/back".
In other words, I have to remove the last word with '/'. Initial string always has a different length, but the format is the same "word1/word2/word3/word4/word5...."
var x = "home/back/step";
var splitted = x.split("/");
splitted.pop();
var str = splitted.join("/");
console.log(str);
Take the string and split using ("/"), then remove the last element of array and re-join with ("/")
Use substr and remove everything after the last /
let str = "home/back/step";
let result = str.substr(0, str.lastIndexOf("/"));
console.log(result);
You could use arrays to remove the last word
const text = 'home/back/step';
const removeLastWord = s =>{
let a = s.split('/');
a.pop();
return a.join('/');
}
console.log(removeLastWord(text));
Seems I got a solution
var s = "your/string/fft";
var withoutLastChunk = s.slice(0, s.lastIndexOf("/"));
console.log(withoutLastChunk)
You can turn a string in javascript into an array of values using the split() function. (pass it the value you want to split on)
var inputString = 'home/back/step'
var arrayOfValues = inputString.split('/');
Once you have an array, you can remove the final value using pop()
arrayOfValues.pop()
You can convert an array back to a string with the join function (pass it the character to place in between your values)
return arrayOfValues.join('/')
The final function would look like:
function cutString(inputString) {
var arrayOfValues = inputString.split('/')
arrayOfValues.pop()
return arrayOfValues.join('/')
}
console.log(cutString('home/back/step'))
You can split the string on the '/', remove the last element with pop() and then join again the elements with '/'.
Something like:
str.split('/');
str.pop();
str.join('/');
Where str is the variable with your text.
I have a String such as ABC_DEF_GHI.
Using JavaScript I need to get everything after the first _ (that is, DEF_GHI in this case). There could be any number of _ in the String.
If I do something like
var str = "ABC_DEF_GHI_JKL";
var n = str.lastIndexOf('_');
var output = str.substring(n + 1);
This would give me everything after the last underscore. However, I need everything after the first underscore. Couldn't find a method such as firstIndexOf which would give me everything after the first _
You should replace your lastIndexOf() by indexOf() which will take the first occurrence
var str = "ABC_DEF_GHI_JKL";
var n = str.indexOf('_');
var output = str.substring(n + 1);
console.log(output);
var str = "ABC_DEF_GHI",
pos = str.indexOf("_");
result = str.slice(pos+1);
console.log(result);
console.log("ABC_DEF_GHI_JKL".split('_').slice(1).join('_'));
The above simply split's your string into an array, splitting at the _, and then drops the first array element, and then joins them back together with _.
One way of doing this is
var str = "ABC_DEF_GHI",
pos = str.indexOf("_");
result = str.slice(pos+1);
console.log(result);
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);
I have string like #ls/?folder_path=home/videos/
how i can find last text from string? this place is videos
other strings like
#ls/?folder_path=home/videos/
#ls/?folder_path=home/videos/test/testt/
#ls/?folder_path=seff/test/home/videos/
We could use a few more example strings, but based off of your one and only example, here's a rough regex to get you started:
.*?/\?.*?/(.*?)\//
EDIT:
Based on your extended examples:
.*?/\?.*/(.*?)\//
This regex will consume text until the second to last / and capture until the last / in the string.
This will work even if the string doesn't end in /
var str;
var re = /\w+(?=\/?$)/;
str = "#ls/?folder_path=home/videos/"
str.match(re) ; //# => videos
str = "#ls/?folder_path=home/videos/test/testt/"
str.match(re) ; //# => testt
str = "#ls/?folder_path=seff/test/home/videos/"
str.match(re) ; //# => videos
str = "#ls/?folder_path=home/videos/test/testt"
str.match(re) ; //# => testt
\/([^\/]*)\/?$
This regex will match all non / between the last two /. Where the last / is optional. The $ is matching the end of the string.
Your resulting string is then in the first capturing group (because of the ()) $1
You can test it here
There are many ways to do this. One of them:
var str = '#ls/?folder_path=home/videos/'.replace(/\/$/,'');
alert(str.substr(str.lastIndexOf('/')+1)); //=> videos
Alternative without using replace
var str = '#ls/?folder_path=home/videos/'
,str = str.substr(0,str.length-1)
,str = str.substr(str.lastIndexOf('/')+1);
alert(str); //=> videos
If your data is consistent like this string, this is a simple split based way to retreive
your required string: http://jsfiddle.net/EEkLP/
var str="#ls/?folder_path=home/videos/";
var strArr = str.split("/");
alert(strArr[strArr.length-2]);
If it always ends with / then this will works.
var str = '#ls/?folder_path=home/videos/';
var arr = str.split('/');
var index = arr.length-2;
console.log(arr[index]);
If the last word always enclosed with forward slashes, then you can try this -
".+\/([^\/]+)\/$"
or in regex notation
/.+\/([^\/]+)\/$/