cut the string in javascript [closed] - javascript
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
cut the string from last '/' to '.html'
I have a string like that
"/Views/GS/stockView.html"
My need the name "stockView"
How can I cut the name from a string?
Thanks.
a = "/Views/GS/stockView.html";
a.split('/').pop().split(".")[0];
Demo
use indexOf() and lastIndexOf() method, like
var str = "/Views/GS/stockView.html";
var slashPos = str.lastIndexOf('/');
var dotPos = str.indexOf('.', slashPos + 1);
var result = str.substring(slashPos + 1, dotPos);
Try using RegExp:
var view = function(str) {
return str.match(/\/(\w*)\./)[1];//finds a word between `/` and `.`
};
console.log(view("/Views/GS/stockView.html"));
console.log(view("/Views/fs/inventView.html"));
console.log(view("/Views/fs/p1/showView.jsp"));
console.log(view("/Views/fs/p2/showView123.aspx"));
Open console
Try this
var msg = "/Views/GS/stockView.html";
var startIndex = -1;
var endIndex=-1;
var length = msg.length;
for (var i = length - 1; i >= 0; i--) {
if (msg[i] == '/'){
startIndex=i+1;
break;
}
if(msg[i]==".")
endIndex=i;
}
console.log(msg.substr(startIndex,endIndex-startIndex));
Or try this
var msg = "/Views/GS/stockView.html";
var split=msg.split("/");
split=split[split.length-1].split(".");
console.log(split[0]);
Related
insert a string at a specific position [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago. Improve this question i have an array and i want to insert "ZZ" if the current array value(string) contains "ata", the code should replace at the end of "ata" word. var duplicatesArray = ["abıca","abrık","apşak","abbak","abu","aparma","apalisına","appak","aparmadutı","apşak","apışık","apşak","apışıklık","apışık","apalak","apılamak","apul","apul","apulamak","aparmak","at","arkasına","gelmek","ata","atabeg","at","eri","at","ağaç","at","oğlanı","at","akdarıcı","at","otayıcı","at","uşağı","at","oğlanı","at","oynağı","at","bırakmak","at","boynuna","düşmek","at","boynuna","düşmek","at","cıvlandurmak","at","çapmak","at","çapmak","at","depretmek","at","depmek","atı","doldurmak","at","segirtmek","ateş","evi","ateş","göyniigi","atışmak","ateşe","urmak","ateşe","nal","komak","at","şalmak","at","şalmak","at","tonı","at","kaşnısı","at","kaldırmak","at","kulağı","at","koparmals","at","koşmak","at","kulağı","götliği","atlaz","atlandurmak","atlandurmak","atlanmak","atlu","azuğı","atımı","yir","ata","atalar","atıcıduğı","aç","itmek","acıtğan","acıtmak","aç","dirilmek","acır","acırak","acışıklık","acışmak","aç","tutmak" ]; var uniqueArray = duplicatesArray.filter(function(elem, pos) { return duplicatesArray.indexOf(elem) == pos; }); for (var i = 0; i < uniqueArray.length; i++) { var st = uniqueArray[i]; if((st.endsWith("mak")==false) && (st.endsWith("mek")== false) && (st.length>3)) { var b = "ata"; var insert = "ZZ"; var position = st.indexOf("b"); st = st.slice(0, position) + insert + st.slice(position); document.writeln(st); document.write("<br>"); } }
I may need to edit this answer later once some details have been clarified, but it seems like you should use the .map() method on your uniqueArray. This code will walk through each word in the list and either let it unchanged or apply the replacement if all conditions are fulfilled. // using a shorter, already deduplicated list for sake of clarity var uniqueArray = [ "abıca","gelmek","ata","atabeg","at","eri","yir","atalar","tutmak" ]; var result = uniqueArray.map(function(word) { return ( !word.endsWith("mak") && !word.endsWith("mek") && word.length > 3 ? word.replace(/ata/, "ataZZ") : word ); }); console.log(result);
I am right or wrong? :) var initialArray = ["abıca","abrık","apşak","abbak","abu","aparma","apalisına","appak","aparmadutı","apşak","apışık","apşak","apışıklık","apışık","apalak","apılamak","apul","apul","apulamak","aparmak","at","arkasına","gelmek","ata","atabeg","at","eri","at","ağaç","at","oğlanı","at","akdarıcı","at","otayıcı","at","uşağı","at","oğlanı","at","oynağı","at","bırakmak","at","boynuna","düşmek","at","boynuna","düşmek","at","cıvlandurmak","at","çapmak","at","çapmak","at","depretmek","at","depmek","atı","doldurmak","at","segirtmek","ateş","evi","ateş","göyniigi","atışmak","ateşe","urmak","ateşe","nal","komak","at","şalmak","at","şalmak","at","tonı","at","kaşnısı","at","kaldırmak","at","kulağı","at","koparmals","at","koşmak","at","kulağı","götliği","atlaz","atlandurmak","atlandurmak","atlanmak","atlu","azuğı","atımı","yir","ata","atalar","atıcıduğı","aç","itmek","acıtğan","acıtmak","aç","dirilmek","acır","acırak","acışıklık","acışmak","aç","tutmak"]; var newArray = [] var regexp = /(ata)(.*)?/; for (var i = 0; i< initialArray.length; i += 1) { newArray.push(initialArray[i].replace(regexp, "$1ZZ$2")) } console.log(newArray) // ... "gelmek", "ataZZ", "ataZZbeg" ...
Put the .length before a string in JavaScript [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 7 years ago. Improve this question Code for Length of string to be displayed in front of actual string. Example length: 11, "This string" Thank you.
var str = 'Hello World'; console.log('length: ' + str.length + ', "' + str + '"'); Evaluates to: length: 11, "Hello World"
Solution : var str = "This string"; var n = str.length; var displaystring = 'length: '+ n + ', "' + str + '"' //display the above string however you want. //example below to show in a control name demo document.getElementById("demo").innerHTML = displaystring;
I agree with #pointy that its not clear what you really need done but this might give you some idea var str1 = "This string"; var str2 = str1.length; var result = str2.concat(str1);
Javascript - How to get an array of indexes of all matches of a RegExp against a string? [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 8 years ago. Improve this question I have this simple problem, and I want a simple solution, if it exists. Suppose I have the following string: var myString = "IS is iS Is"; var myArray = []; I want to get an array of size 4, where: myArray[0] = 0; myArray[1] = 3; myArray[2] = 6; myArray[3] = 9;
You can; var myString = "IS is iS Is"; var myArray = []; var re = /\b(is)\b/ig; match = re.exec(myString); while (match != null) { myArray.push(match.index); match = re.exec(myString); } document.write(myArray);
Find last element in a div and it's index in an array [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 9 years ago. Improve this question Double clicking on an image in the #train div will delete that image and all the images to thr right of it. var d should return the last image in the train div and var g should return the index of that image in the main_pics array. $(document).ready(function () { var main_pics = ["AN.gif", "BN.gif", "CN.gif", "DN.gif", "EN.gif", "GN.gif"]; var starting_pics = ["AN.gif", "CN.gif", "EN.gif"]; var i = 0; for (i = 0; i < starting_pics.length; i++) { $("<img/>").attr("src", "images/" + starting_pics[i]).appendTo("#main").addClass("pics"); } // Code not relevant to the question. $("#train").on("dblclick", ".pics", function () { var l = $("#train").children(".pics").length; var c = $(this).index(); $("#train").children().slice(c, l).remove(); var d = $("#train").children(".pics").last() alert(d); var g = $.inArray(d.src.split("/").pop(), main_pics); alert(g); }); });
Here is your fix: var d = $("#train .pics").last(); var g = $.inArray(d.prop('src').split("/").pop(), main_pics);
javascript: splitting a string (yet preserving the spaces) [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist Closed 9 years ago. Improve this question How do I split a string like this "please help me " so that I get an array like this: ["please ","help ","me "] In other words,I get an array which preserves the space (or spaces) Thanks
something like : var str = "please help me "; var split = str.split(/(\S+\s+)/).filter(function(n) {return n}); FIDDLE
this is tricky without using a function; var temp = "", outputArray = [], text = "please help me ".split(""); for(i=0; i < text.length; i++) { console.log(typeof text[i+1]) if(text[i] === " " && (text[i+1] !== " " || typeof text[i+1] === "undefined")) { outputArray.push(temp+=text[i]); temp=""; } else { temp+=text[i]; } } console.log(outputArray); I don't think a simple regex can help this out. you can use prototype to use it like a native code... String.prototype.splitPreserve = function(seperator) { var temp = "", outputArray = [], text = this.split(""); for(i=0; i < text.length; i++) { console.log(typeof text[i+1]) if(text[i] === seperator && (text[i+1] !== seperator || typeof text[i+1] === "undefined")) { outputArray.push(temp+=text[i]); temp=""; } else { temp+=text[i]; } } return outputArray; } console.log("please help me ".splitPreserve(" "));