I have to get the value from a textarea using jQuery and count the number of newlines there. I'd like to do this using a regex-expression. Does anyone know how to do that?
regex does not have count. better use array like this
var val = textarea.value;
var arr = val.split(/[\n\r]/g);
var count = arr.length;
you could condense this in less rows and vars...
var count = $('textarea').val().split(/[\n\r]/g).length;
http://jsfiddle.net/qxKmW/1/
$(document).ready(function() {
var str = $("#txtField").val();
parts = str.split(/[\n\r]/g);
var newline_count = parts.length;
alert("Count: " + newline_count);
});
Related
I'm trying to make a script that changes text into these cool looking letters, it looks like everything should be working but when I try to send the replaced message it gives me a character like a white question mark on a black background. This: �
Here's the code:
var mm = "test";
var alphabet = "🇦🇧🇨🇩🇪🇫🇬🇭🇮🇯🇰🇱🇲🇳🇴🇵🇶🇷🇸🇹🇺🇻🇼🇽🇾🇿";
var nalphabet = "abcdefghijklmnopqrstuvwxyz";
for(var z in mm){
var x = nalphabet.indexOf(mm[z].toLowerCase());
var ool = alphabet[x];
msg.channel.sendMessage(ool);
}
This is all about the coding of characters. Each of your cool-looking letters has length 2. So when you try to get such character directly by index, you receive just a half of it. As a solution you can try to join two sibling characters. Something like this
var mm = "test";
var alphabet = "🇦🇧🇨🇩🇪🇫🇬🇭🇮🇯🇰🇱🇲🇳🇴🇵🇶🇷🇸🇹🇺🇻🇼🇽🇾🇿";
var nalphabet = "abcdefghijklmnopqrstuvwxyz";
for(var z in mm){
var x = nalphabet.indexOf(mm[z].toLowerCase());
var ool = alphabet[x * 2] + alphabet[x * 2 + 1];
msg.channel.sendMessage(ool);
}
Try this one:
var message = "test";
var alphabet = ["🇦","🇧","🇨","🇩","🇪",
"🇫","🇬","🇭","🇮","🇯",
"🇰","🇱","🇲","🇳","🇴",
"🇵","🇶","🇷","🇸","🇹",
"🇺","🇻","🇼","🇽","🇾","🇿"];
var nalphabet = "abcdefghijklmnopqrstuvwxyz";
for(var letter in message) {
var x = nalphabet.indexOf(message[letter].toLowerCase());
var ool = alphabet[x];
document.write(ool);
}
Here is a Fiddle.
Instead of "var instance = ..." adding the two values it concatenates them. Can anyone suggest what I need to fix?
I'm trying to add "var startingEmail" value and "var k".
Thank you for your help!
var startingEmail = sheet.getRange("C2").getDisplayValue();
var numEmails = sheet.getRange("E2").getDisplayValue();
var max = numEmails;
for (var k = 0; k<max; ++k){
var threads = GmailApp.getInboxThreads(startingEmail,max)[k]; //get max 50 threads starting at most recent thread
var messages = threads.getMessages()[0];
var sndr;
var rcpnt;
var srAry = [];
var sndr = messages.getFrom().replace(/^.+<([^>]+)>$/, "$1"); //http://stackoverflow.com/questions/26242591/is-there-a-way-to-get-the-specific-email-address-from-a-gmail-message-object-in
var sndrLower = sndr.toLowerCase;
var rcpnt = messages.getTo().replace(/^.+<([^>]+)>$/, "$1");
var rcpntLower = rcpnt.toLowerCase;
var cc = messages.getCc().replace(/^.+<([^>]+)>$/, "$1");
var ccLower = cc.toLowerCase;
//srAry.push(sndr);
//srAry.push(rcpnt);
//srAry.push(cc);
var isIn = joinAddr.search(sndr || rcpnt);
if(isIn == -1){
var instance = k;
I can't see the example in your code but it sounds like you can just wrap Number() around your variable and it will perform the type conversion so the code will perform the math instead of concatenating as strings.
So i have this string
first €999, second €111
Im trying to make an array that looks like this (numbers after every €)
999,111
Edit:
Yes i have tried to split it but wont work. i tried to look it up on google and found something with indexof but that only returned the number of the last €.
rowData[2].split('€').map(Number);
parseInt(rowData[2].replace(/[^0-9\.]/g, ''), 10);
split(rowData[2].indexOf("€") + 1);
The numbers are variable.
var input ="first €999, second €111";
var output=[];
var arr = input.split(",");
for(var i=0;i<arr.length;i++)
{
output.push(parseInt(arr[i]));
}
var output_string = output.stingify();
console.log(output); //Output Array
console.log(output_string); //Output String
If the numbers will always be of 3 digits in length, you can do this. If not, you need to specify a bit more.
var string = "€999, second €111";
var temp = [];
var digitArray = [];
temp = string.split(",");
for(var i=0;i<temp.length,i++){
digitArray.push(temp[i].substring(temp[i].indexOf("€"),3));
}
//digitArray now contains [999,111];
Edit, based on your requirement of variable digit lengths
var string = "€999, second €111, third €32342";
var temp = [];
var digitArray = [];
temp = string.split(",");
for(var i=0;i<temp.length,i++){
digitArray.push(temp[i].replace(/^\D+/g, '')); //Replace all non digits with empty.
}
//digitArray now contains [999,111,32342]
I have an object in javascript -:
var array = {"1":"John","2":"Caprio","3":"David","4":"Edward"}
I want to do this -:
var message = 'Wats up David#1. Are you with David#5 or Caprio#89';
$.each(array, function(key, value){
var matchMe=value+'#'+anywholenumber;
if(message.match(matcheMe))
{
var ge = new RegExp(matchMe, 'g');
message = message.replace(ge,'['+matchMe+']');
} });
How do i do it? Thanks a lot for help
The code you have would replace every number with the first element (john) if it were working.
I think you want this:
var arr = array("John","Caprio","David","Edward","Suzy");
var message = 'Wats up David#1. Are you with David#5 or Caprio#2';
for (i=0; i < arr.length; i++) {
var re = new RegExp(i, 'g');
message = message.replace(re, arr[i]);
}
I have a file full with text in the following format:
(ignoring the fact that it is CSS) I need to get the string between the two | characters and each time, do something:
<div id="unused">
|#main|
#header|
.bananas|
#nav|
etc
</div>
The code I have is this:
var test_str = $('#unused').text();
var start_pos = test_str.indexOf('|') + 1;
var end_pos = test_str.indexOf('|',start_pos);
var text_to_get = test_str.substring(start_pos,end_pos);
//I want to do something with each string here
This just gets the first string. How can I add logic in there to do something for each string?
You can use split method to get array of strings between |
Live Demo
arr = $('#unused').text().split('|');
You can split like
var my_splitted_var = $('#unused').text().split('|');
One way;
$.each($("#unused").text().split("|"), function(ix, val) {
val = $.trim(val); //remove \r|\n
if (val !== "")
alert(val);
});
One way :
var test_str = $('#unused').text();
while(!test_str.indexOf('|'))
{
var start_pos = test_str.indexOf('|') + 1;
var end_pos = test_str.indexOf('|',start_pos);
var text_to_get = test_str.substring(start_pos,end_pos);
test_str = test_str.slice(end_pos,test_str.length);
}
RegExp-Version:
LIVE DEMO (jsfiddle.net)
var trimmedHtml = $("#unused").html().replace(/\s/g, '');
var result = new Array();
var regExp = /\|(.+?)(?=\|)/g;
var match = regExp.exec(trimmedHtml);
result.push(match[1]);
while (match != null) {
match = regExp.exec(trimmedHtml);
if (match != null) result.push(match[1]);
}
alert(result);
So you only get the elements BETWEEN the pipes (|).
In my example I pushed every matching result to an array. You can now iterate over it to get your result.