Put the .length before a string in JavaScript [closed] - javascript

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);

Related

Merge Numbers Textarea Automatic [closed]

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);

Split a string between special characters [closed]

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 4 years ago.
Improve this question
I would like to split a string between these ''' characters.
My string looks like this:
const str = " My random text '''tag1''' '''tag2''' ";
and the output should look like this: ["tag1", "tag2"]
Use a regular expression with a capture group and RegEx.exec() to extract any substrings between three apostrophes. The pattern uses lazy matching (.*?) to match any sequence of characters (including none) between the apostrophes.
const str = " My random text '''tag1''' '''tag2''' ";
re = /'''(.*?)'''/g;
matches = [];
while (match = re.exec(str)) {
matches.push(match[1]);
}
console.log(matches);
Output
[ 'tag1', 'tag2' ]
I think a short and sweet method would be :
var regex = /(?<=''')\w+(?=''')/mgi;
var matches = "My random text '''tag1''' '''tag2'''".match(regex);
with a result of : (2) ["tag1", "tag2"]
It matches everything between any occurence of '''.
You can use a function like this:
function getBetween(content, start, end) {
var result = [];
var r = content.split(start);
for (var i = 1; i < r.length; i++) {
result.push(r[i].split(end)[0]);
if (i < r.length - 1) { i++; }
}
return result;
}
Usage:
var result = getBetween(str, "'''", "'''");
which results in
["tag1", "tag2"]
Proof
const str1 = " My random text '''tag1''' '''tag2''' ";
const str2 = "String '''tag1''' this will work '''tag2'''.";
const str3 = "'''tag1''' '''tag2''' sup man '''tag3'''";
function getBetween(content, start, end) {
var result = [];
var r = content.split(start);
for (var i = 1; i < r.length; i++) {
result.push(r[i].split(end)[0]);
if (i < r.length - 1) { i++; }
}
return result;
}
var result1 = getBetween(str1, "'''", "'''");
var result2 = getBetween(str2, "'''", "'''");
var result3 = getBetween(str3, "'''", "'''");
console.log(result1);
console.log(result2);
console.log(result3);
Hope this helps
Try using template literal like below:
str.split(`'''`);
The result may look like this:
[" My random text ", "tag1", " ", "tag2", " "]
Hope this help ;)

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);

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(" "));

Categories