var str = '#/promotionalMailer/test1';
output should be ==> #/promotionalMailer
I want the string before the second slash '/'
I have tried this so far:
var str = '#/promotionalMailer/test1';
var match = str.match(/([^\/]*\/){2}/)[0];
alert(match);
But it comes with the second slash.
try split, slice and join
var str = '#/promotionalMailer/test1';
console.log( str.split("/").slice(0,2).join("/"));
For example,
var str = '#/promotionalMailer/test1/foo/bar/baz';
result = str.split('/').slice(0, 2).join('/')
document.write('<pre>'+JSON.stringify(result,0,3));
If you want regexes, then
var str = '#/promotionalMailer/test1/foo/bar/baz';
result = str.match(/[^\/]*\/[^\/]*/)[0]
document.write('<pre>'+JSON.stringify(result,0,3));
Related
How can i correctly cut out letter "v" and alert str without "v"
var str = "Javascript";
var cut = str.substring(2,3);
var str = "Javascript";
var cut = str.substring(0,2) + str.substring(3);
alert(cut);
You're using the right tool (String#substring). You need two substrings that you put back together, the "Ja" and the "ascript". So:
var str = "Javascript";
var cut = str.substring(0, 2) + str.substring(3);
alert(cut);
Another option would be String#replace, which will replace the first occurrence of what you give it unless you tell it to do it globally with a regex and the g flag (which we won't, because we just want to remove that one v):
var str = "Javascript";
var cut = str.replace("v", "");
alert(cut);
Just for fun, there is another way, but it's a bit silly: You can split the string into an array of single-character strings, remove the third entry from the array, and then join it back together:
var str = "Javascript";
var cut = str.split("").filter(function(_, index) {
return index != 2;
}).join("");
alert(cut);
or
var str = "Javascript";
var cut = str.split("");
cut.splice(2, 1); // Delete 1 entry at index 2
cut = cut.join("");
alert(cut);
...but again, that's a bit silly. :-)
use replace method
var str = "Javascript";
str = str.replace("v", "");
alert(str);
I have a String "SHELF-2-1-1-2-1", I need to remove "2" from that string and want the output to be "SHELF-1-1-2-1"
I tried:
var str = "SHELF-2-1-1-2-1";
var res = str.split("-");
How can I join the array to get "SHELF-1-1-2-1"?
This would work:
var str = "SHELF-2-1-1".split('-2').join('');
Sounds like you want to do a replace... Try:
var res = str.replace('-2', '');
var str = "SHELF-2-1-1";
var res = str.split("-");
res.pop(res.indexOf('2'));
var newStr = res.join('-');
This should also work for your updated question, as it will only remove the first 2 from the string
let str = "Hello India";
let split_str = str.split("");
console.log(split_str);
let join_arr = split_str.join("");
console.log(join_arr);
I have string
var str = "Ahora MXN$1,709.05" and wanted to get only
"MXN$1,709.05" from this.
Can someone please help me?
You can use substring or replace. With replace you are going to replace something with nothing.
replace
var str = 'Ahora MXN$1,709.05';
var sub = 'Ahora ';
var res = str.replace(sub,'');
substring
var str = 'Ahora MXN$1,709.05';
var sub = 'Ahora ';
var res = str.substring(sub.length);
JsFiddle
You can use either substring or Regex
Using substring
var str = "Ahora MXN$1,709.05";
var result = str.substring('Ahora '.length);
console.log(result);
Using Regex
var str = "Ahora MXN$1,709.05";
var myRegexp = /Ahora\s(.*?)(?:\s|$)/g;
var match = myRegexp.exec(str);
console.log(match[1]);
I have some strings like:
str1 = "Point[A,B]"
str2 = "Segment[A,B]"
str3 = "Circle[C,D]"
str4 = "Point[Q,L]"
Now I want to have function that gives me character after "[" and the character before "]". How could I make something like that ?
try this one...
var str = "Point[A,B]";
var start_pos = str.indexOf('[') + 1;
var end_pos = str.indexOf(']',start_pos);
var text_to_get = str.substring(start_pos,end_pos)
alert(text_to_get);
You'd need regex to do that
var matches = /\[(.*?)\]/.exec(str1);
alert(matches[1]);
You can use match() to extract the characters:
str.match(/\[(.*)\]/)[1]
A safer way would be:
var matches = str.match(/\[(.*)\]/);
if(matches) {
var chars = matches[1];
}
Here's an approach which avoids regex.
var str = "Point[A,B]";
var afterOpenBracket = str.split("[")[1]; // returns "A,B]"
var bracketContents = afterOpenBracket.split("]")[0]; // returns "A,B"
There, pretty simple! bracketContents now contains the entirety of the text between the first set of brackets.
We can stop here, but I'll go a step further anyway and split up the parameters.
var parameters = bracketContents.split(","); // returns ["A", "B"]
Or in case u have more [A,C,D,B] and don't want to use regex:
var str1 = "Point[A,C,D,B]";
function extract(str1){
var a = str1.charAt(str1.indexOf('[')+1);
var b = str1.charAt(str1.indexOf(']')-1);
return [a, b];
//or
//a.concat(b); //to get a string with that values
}
console.log(extract(str1));
How to split a string in JavaScript with the "," as seperator?
var splitString = yourstring.split(',');
See split
var str = "test,test1,test2";
var arrStr = str.split(',');
var arrLength = arrStr.length; //returns 3
Use split to split your string:
"foo,bar,baz".split(",") // returns ["foo","bar","baz"]
var expression = "h,e,l,l,o";
var tokens = expression.split("\,");
alert(tokens[0]);// will return h