selecting second string point using js substring - javascript

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

Related

How to get a sub-string after the last specified char ? 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)

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(/^[^.]*. */, "");

Only extract digits with javascript

I need to extract digits only (at least 2 but not more than 3) from an option text in a drop down list and get that shown in an input field. I've read about regexp (http://www.javascriptkit.com/jsref/regexp.shtml) and thought i had it all figured out. But i just can't seem to get it to work.
<script>
function copyEbc() {
var a = document.getElementById("malt"); // the <select>
var onlydig = /\d{1,3}/ // regexp
var option = a.options[a.selectedIndex].text(onlydig); // regexp on options
var txt = document.getElementById("ebc").value;
txt = txt + option;
document.getElementById("ebc").value = txt;
}
</script>
Example, I only want "4" from a selected option with the text "Pilsner 4 EBC".
Am I completely lost here?
Input much appreciated, cheers
You're trying to call the text value as a function (your JS console has probably complained about this).
Match against the regular expression, instead. match[0] will contain the matched text:
var option = a.options[a.selectedIndex].text.match(onlydig)[0];
to simply extract the number(s) from a string use match:
var numberPattern = /\d+/g;
var foo = 'Pilsner 4 EBC';
var numbers = foo.match(numberPattern);
alert(numbers); // alerts the array of numbers
You need to use the "match" method of the regexp object. AFAIK the "text" method you are using do not exist.

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(/,$/,'')

Categories

Resources