Merge Numbers Textarea Automatic [closed] - javascript
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
123456 12-12 3456
How do I automatically convert the text into the Textarea field?
123456 | 12 | 12 | 3456
automatically corrected.
How can I do this in Textarea?
You can attach a keyup event listener on your textarea and modifying it's content by splitting based on 1 or more non-digit characters and joining them with your desired pipe(|) symbol.
document.getElementById('some_id').addEventListener('keyup',function(){
var content = this.value.split(/[^\d]+/);
this.value = content.join(" | ");
});
<textarea rows='10' cols='50' id='some_id'>
</textarea>
Ok, so as discussed in the comments, for your credit card formatting, you can do something like below:
document.getElementById('some_id').addEventListener('keyup',function(){
var content = this.value;
var new_content = '';
var temp_content = '';
var current_length = 0;
for(var i=0;i<content.length;++i){
if('0123456789'.indexOf(content.charAt(i)) != -1){
temp_content += content.charAt(i);
current_length++;
}
if(current_length === 25){
new_content += insertPipeForCreditCardFormat(temp_content) + '\n';
temp_content = '';
current_length = 0;
}
}
this.value = new_content + temp_content;
});
function insertPipeForCreditCardFormat(credit_card){
var pipe_indices = new Set([16,18,22]);
var card_format = '';
for(var i=0;i<credit_card.length;++i){
if(pipe_indices.has(i)){
card_format += '|';
}
card_format += credit_card.charAt(i);
}
return card_format;
}
<textarea id='some_id' rows='10' cols='50'></textarea>
Use replace:
const str = "123456 12-12 3456";
const replaced = str.replace(/\-|\s/g, " | ");
console.log(replaced);
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);
cut the string in javascript [closed]
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]);
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);