How to get a sub-string after the last specified char ? Javascript - javascript

lets say I have the following string :
var text = "A_B_C_190"
I want to be able to extract the number at the end (the last 3 chars after the last _ )
I tired :
text.substr(text.indexOf('_'), -1)
but that gave me null

You can use string.prototype.split and array.prototype.pop:
var text = "A_B_C_190";
console.log(text.split('_').pop());

You have to use lastIndexOf method.
var text = "A_B_C_190"
ans = text.substr(text.lastIndexOf('_')+1)
console.log(ans)
If you want to convert it to an integer, use parseInt(ans)

Related

Cut text using jquery

How to cut text using jQuery? For example :
if output like :
new/2016/songs1.mp3
new/2015/songsx.mp3
new/songs3.mp3
Need output :
songs1.mp3
songsx.mp3
songs3.mp3
I want to put only file name with extension like songs-name.mp3 not directory, so i want to cut this using jQuery.
split it and take the last item
var str = "new/2016/songs1.mp3";
var items = str.split( "/" );
alert(items[items.length - 1 ]);
or simply
alert( str.split("/").pop() );
if you want to remove the rest of the text then
var str = "new/2016/songs1.mp3";
var items = str.split( "/" );
str = items[items.length - 1 ];
or
str = str.split("/").pop();
Check it out:
Here i use split function to split the string and then its return an array.
From that array we have to get the last portion i.e, Song name, So we have to get the length of that array.
After that we alert the array with the index of last portion.
var str = "new/2016/songs1.mp3";
var arr = str.split("/");
alert(arr[(arr.length) - 1]);
try this
var filepath = "new/2015/songsx.mp3";
console.log(filepath.slice(filepath.lastIndexOf("/")+1));
Using the slice method (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) you can get a part of a string.
Using the lastIndexOf method (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) returns the index of the last occurrence of the given string (or -1 if given string does not occur in the string).
So what we are doing is getting the slice if the file path starting with the first character after the las "/" in the filepath variable.

Trim a variable's value until it reaches to a certain character

so my idea is like this..
var songList = ["1. somesong.mid","13. abcdef.mid","153. acde.mid"];
var newString = myString.substr(4); // i want this to dynamically trim the numbers till it has reached the .
// but i wanted the 1. 13. 153. and so on removed.
// i have more value's in my array with different 'numbers' in the beginning
so im having trouble with this can anyone help me find a more simple solution which dynamically chop's down the first character's till the '.' ?
You can do something like
var songList = ["1. somesong.mid","13. abcdef.mid","153. acde.mid"];
songList.forEach(function(value, i){
songList[i] = value.replace(/\d+\./, ''); //value.substr(value.indexOf('.') + 1)
});
Demo: Fiddle
You can use the string .match() method to extract the part up to and including the first . as follows:
var newString = myString.match(/[^.]*./)[0];
That assumes that there will be a match. If you need to allow for no match occurring then perhaps:
var newString = (myString.match(/[^.]*./) || [myString])[0];
If you're saying you want to remove the numbers and keep the rest of the string, then a simple .replace() will do it:
var newString = myString.replace(/^[^.]*. */, "");

Using JS Regex to obtain the first value proceeding a string with dynamic trailing values

Given the following string variations:
var string = "groups/Da12312a"
var string = "groups/Da12312a/search"
var string = "groups/Da12312a/search/sam"
var string = "groups/3131"
var string = "groups/444/search"
var string = "groups/123asdadsZad/search/sam"
How can I get back just the value following groups/ and ending at the first '/'?
desired output:
Da12312a
Da12312a
Da12312a
3131
444
123asdadsZad
Using jQuery or JavaScript? Thanks
You can use the split method.
string.split("/")[1];
I would use a simple regular expression.
var str = 'groups/foo';
var matches = /groups\/([^/]*)/.exec(str);
matches will now contain an array where index 0 is "groups/100" and index 1 is "foo".
["groups/foo", "foo"]
If you want a regex, you can use the following:
/^[^\/]*?\/([^\/]*).*/
Basically the captured group yields your desired result.
Use like:
"groups/Da12312a/search".match(/^[^\/]*?\/([^\/]*).*/)
["groups/Da12312a/search", "Da12312a"]
"groups/Da12312a".match(/^[^\/]*?\/([^\/]*).*/)
["groups/Da12312a", "Da12312a"]
"groups/3131".match(/^[^\/]*?\/([^\/]*).*/)
["groups/3131", "3131"]
As you can see, in each of the cases the array index [1] is your result.
Hope that helps.
var output = string.split('/')[1];
String.split reference.
Here's a demo with your provided examples and output.

What is the correct pattern for splitting a string in javascript, leaving just a-z words

I have the next code that was given to me to split up a string into an array.
var chk = str.split(/[^a-z']+/i);
The problem I'm having with this solution is that if the string has a period in the end, it's being replaced with ","
For example:
If I have the next string: "hi,all-I'm-glad."
The solution above results: "hi,all,I'm,glad," (notice the "," in the end).
I need that the new string will be: "hi,all,I'm,glad"
How can I acheive it ?
Check for a . being the last character and remove it first
var str = "hi,all-I'm-glad. that you, can help,me. that-doesn't make any-sense, I know.";
if(str.charAt( str.length-1 ) == ".") {
str = str.substring(0,str.length-1);
}
var chk = str.split(/[^a-z']+/i);
console.log(chk);
var chk = str.match(/[a-z']+/gi);
console.log(chk);
You could check to see if the last element of your string array returns an empty string and remove that element
if (chk[chk.length-1] == "")
{
chk.pop();
}
var chk="to.to.".split(/[^a-z']+/i); if(chk[chk.length-1].length==0){chk.pop()}; console.log(chk);
To remove the last value of your array using pop if this one is empty.
You can utilize the pure regex power:
"hi,all-I'm-glad. that you, can help,me. that-doesn't make any-sense, I know.".replace(/[\-\.\s]/g, ',').replace(/,{2,}/g, ',').replace(/,$/,'')

selecting second string point using js substring

i want to select a sting from the long para. it has number of dot('.')s. i want to trim the word from the second one, is it any way to do this?
example
var name = "one.two.three";
name.substring(0,name.indexOf('.'))
name.substring(0,name.lastIndexOf('.'))
from above trimming in case if i use indexOf it gives first word (one), if i use lastIndex of it gives the word (three), but i need to select the second one, to get value as 'second'
how can i trim this using indexOf method? or to select multicombination strings like one.three or one.two, or two.three?
thanks in advance!
use string.split.
e.g.
name.split(".")[1]
var name="one.two.three";
var result=name.split(".").slice(0,2).join(".");
Example:
"".split(".").slice(0,2).join(".") // return ""
"one".split(".").slice(0,2).join(".") // return "one"
"one.two".split(".").slice(0,2).join(".") // return "one.two"
"one.two.three".split(".").slice(0,2).join(".") // return "one.two"
"one.two.three.four.five".split(".").slice(0,2).join(".") // return "one.two"
Is that work for you ?
var name = "one.two.three";
var params = name.split('.');
console.log(params[1]);
use Split
var name = "one.two.three";
var output = name.split('.');
alert(output[1]);
example here

Categories

Resources